Skip to content

Add Paint House III.cpp file #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Leetcode/Paint_House_III.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Question Link: https://leetcode.com/problems/paint-house-iii/
*/


class Solution {
public:

int dp[103][103][103];

int help(vector<int> &houses, vector<vector<int>> &cost, int m, int n, int target, int i, int prev) {

if (i == m) {
if (!target) return 0;
else return 1e9;
}

if (target < 0) return 1e9;

if (dp[i][prev][target] != -1) return dp[i][prev][target];


int mn = 1e9;

// case 1:
// no need to colorize
if (houses[i]) {
if (houses[i] != prev) // nbrs increase, hence target decreases
return dp[i][prev][target] = help(houses, cost, m, n, target - 1, i + 1, houses[i]);

else return dp[i][prev][target] = help(houses, cost, m, n, target, i + 1, houses[i]);
}

else {

// case 2: color them

// I need to paint with colors [1,n] such that the current color isn't the same as the prev
// if they are the same, nbrs remain the same (no change in target)
// else, nbrs increase, so target-1

// colors: 1 to n
// cost[i][j]: cost to paint ith building with color j+1


for (int c = 1; c <= n; c++) {
int cur = cost[i][c - 1];

if (prev == c) cur += help(houses, cost, m , n, target, i + 1, c);
else {

// explore the rest (next rows)
cur += help(houses, cost, m , n, target - 1, i + 1, c);
}
mn = min(mn, cur);
}

return dp[i][prev][target] = mn;
}


}

int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {

memset(dp, -1, sizeof(dp));
int mm = help(houses, cost, m, n, target, 0, 0);
return (mm >= 1e9) ? -1 : mm;
}
};
34 changes: 0 additions & 34 deletions Leetcode/average_of_levels_in_binary_tree.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +0,0 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
if (!root) return {};
vector<double> ans;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
vector<int> arr;
double sum = 0;
for (int i = 0; i < size; i++){
TreeNode* temp = q.front();
q.pop();
sum += temp->val;
if (temp->left) q.push(temp->left);
if (temp->right) q.push(temp->right);
}
ans.push_back(sum / size);
}
return ans;
}
};
36 changes: 36 additions & 0 deletions Leetcode/linked_list_cycle_ii.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
https://leetcode.com/problems/linked-list-cycle-ii/description/
*/

ListNode *detectCycle(ListNode *head) {
if(!head)
return NULL;

ListNode *fast = head;
ListNode *slow = head;
bool isCycle = false;

while(fast && fast -> next)
{
slow = slow -> next;
fast = fast -> next -> next;

if(slow == fast)
{
isCycle = true;
break;
}
}

if(!isCycle)
return NULL;

slow = head;
while(slow != fast)
{
slow = slow -> next;
fast = fast -> next;
}

return fast;
}