Skip to content

Commit 1ca55af

Browse files
authored
Merge pull request #88 from atinder11/master
Add Paint House III.cpp file
2 parents 129ed59 + e5d0a18 commit 1ca55af

File tree

3 files changed

+106
-34
lines changed

3 files changed

+106
-34
lines changed

Leetcode/Paint_House_III.cpp

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Question Link: https://leetcode.com/problems/paint-house-iii/
3+
*/
4+
5+
6+
class Solution {
7+
public:
8+
9+
int dp[103][103][103];
10+
11+
int help(vector<int> &houses, vector<vector<int>> &cost, int m, int n, int target, int i, int prev) {
12+
13+
if (i == m) {
14+
if (!target) return 0;
15+
else return 1e9;
16+
}
17+
18+
if (target < 0) return 1e9;
19+
20+
if (dp[i][prev][target] != -1) return dp[i][prev][target];
21+
22+
23+
int mn = 1e9;
24+
25+
// case 1:
26+
// no need to colorize
27+
if (houses[i]) {
28+
if (houses[i] != prev) // nbrs increase, hence target decreases
29+
return dp[i][prev][target] = help(houses, cost, m, n, target - 1, i + 1, houses[i]);
30+
31+
else return dp[i][prev][target] = help(houses, cost, m, n, target, i + 1, houses[i]);
32+
}
33+
34+
else {
35+
36+
// case 2: color them
37+
38+
// I need to paint with colors [1,n] such that the current color isn't the same as the prev
39+
// if they are the same, nbrs remain the same (no change in target)
40+
// else, nbrs increase, so target-1
41+
42+
// colors: 1 to n
43+
// cost[i][j]: cost to paint ith building with color j+1
44+
45+
46+
for (int c = 1; c <= n; c++) {
47+
int cur = cost[i][c - 1];
48+
49+
if (prev == c) cur += help(houses, cost, m , n, target, i + 1, c);
50+
else {
51+
52+
// explore the rest (next rows)
53+
cur += help(houses, cost, m , n, target - 1, i + 1, c);
54+
}
55+
mn = min(mn, cur);
56+
}
57+
58+
return dp[i][prev][target] = mn;
59+
}
60+
61+
62+
}
63+
64+
int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {
65+
66+
memset(dp, -1, sizeof(dp));
67+
int mm = help(houses, cost, m, n, target, 0, 0);
68+
return (mm >= 1e9) ? -1 : mm;
69+
}
70+
};
Original file line numberDiff line numberDiff line change
@@ -1,34 +0,0 @@
1-
/**
2-
* Definition for a binary tree node.
3-
* struct TreeNode {
4-
* int val;
5-
* TreeNode *left;
6-
* TreeNode *right;
7-
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8-
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9-
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10-
* };
11-
*/
12-
class Solution {
13-
public:
14-
vector<double> averageOfLevels(TreeNode* root) {
15-
if (!root) return {};
16-
vector<double> ans;
17-
queue<TreeNode*> q;
18-
q.push(root);
19-
while (!q.empty()) {
20-
int size = q.size();
21-
vector<int> arr;
22-
double sum = 0;
23-
for (int i = 0; i < size; i++){
24-
TreeNode* temp = q.front();
25-
q.pop();
26-
sum += temp->val;
27-
if (temp->left) q.push(temp->left);
28-
if (temp->right) q.push(temp->right);
29-
}
30-
ans.push_back(sum / size);
31-
}
32-
return ans;
33-
}
34-
};

Leetcode/linked_list_cycle_ii.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
https://leetcode.com/problems/linked-list-cycle-ii/description/
3+
*/
4+
5+
ListNode *detectCycle(ListNode *head) {
6+
if(!head)
7+
return NULL;
8+
9+
ListNode *fast = head;
10+
ListNode *slow = head;
11+
bool isCycle = false;
12+
13+
while(fast && fast -> next)
14+
{
15+
slow = slow -> next;
16+
fast = fast -> next -> next;
17+
18+
if(slow == fast)
19+
{
20+
isCycle = true;
21+
break;
22+
}
23+
}
24+
25+
if(!isCycle)
26+
return NULL;
27+
28+
slow = head;
29+
while(slow != fast)
30+
{
31+
slow = slow -> next;
32+
fast = fast -> next;
33+
}
34+
35+
return fast;
36+
}

0 commit comments

Comments
 (0)