From 5cd8486e08227528846e2bb46cde5babd9c6209c Mon Sep 17 00:00:00 2001 From: Atinder Kumar <111070211+atinder11@users.noreply.github.com> Date: Tue, 4 Oct 2022 08:18:10 +0530 Subject: [PATCH 1/4] Add Paint House III.cpp file This program solves issue #48 Question Link: https://leetcode.com/problems/paint-house-iii/ --- Leetcode/Paint_House_III.cpp | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Leetcode/Paint_House_III.cpp diff --git a/Leetcode/Paint_House_III.cpp b/Leetcode/Paint_House_III.cpp new file mode 100644 index 0000000..4f0891c --- /dev/null +++ b/Leetcode/Paint_House_III.cpp @@ -0,0 +1,70 @@ +/* +Question Link: https://leetcode.com/problems/paint-house-iii/ +*/ + + +class Solution { +public: + + int dp[103][103][103]; + + int help(vector &houses, vector> &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& houses, vector>& 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; + } +}; \ No newline at end of file From b2815b8426eac2610051bcaf5c1c409eead92020 Mon Sep 17 00:00:00 2001 From: Atinder Kumar <111070211+atinder11@users.noreply.github.com> Date: Tue, 4 Oct 2022 08:28:20 +0530 Subject: [PATCH 2/4] Add linked_list_cycle_ii.cpp This program fixes issue #60 Question Link: https://leetcode.com/problems/linked-list-cycle-ii/description/ I hope it is helpful and if any feedback or modifications please let me know. I hope you will merge it. --- Leetcode/linked_list_cycle_ii.cpp | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Leetcode/linked_list_cycle_ii.cpp diff --git a/Leetcode/linked_list_cycle_ii.cpp b/Leetcode/linked_list_cycle_ii.cpp new file mode 100644 index 0000000..d0bb1a8 --- /dev/null +++ b/Leetcode/linked_list_cycle_ii.cpp @@ -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; +} \ No newline at end of file From 4f852401f54c35b783f76b22650ae8d14e58460b Mon Sep 17 00:00:00 2001 From: Atinder Kumar <111070211+atinder11@users.noreply.github.com> Date: Tue, 4 Oct 2022 08:36:29 +0530 Subject: [PATCH 3/4] Add Average of level in binary Tree.cpp This program fixes issue #2 Question Link: https://leetcode.com/problems/average-of-levels-in-binary-tree/ I hope it is helpful and if any feedback or modifications please let me know. I hope you will merge it. --- Leetcode/average_of_levels_in_binary_tree.cpp | 34 ------------------- 1 file changed, 34 deletions(-) diff --git a/Leetcode/average_of_levels_in_binary_tree.cpp b/Leetcode/average_of_levels_in_binary_tree.cpp index c770be5..e69de29 100644 --- a/Leetcode/average_of_levels_in_binary_tree.cpp +++ b/Leetcode/average_of_levels_in_binary_tree.cpp @@ -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 averageOfLevels(TreeNode* root) { - if (!root) return {}; - vector ans; - queue q; - q.push(root); - while (!q.empty()) { - int size = q.size(); - vector 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; - } -}; \ No newline at end of file From e5d0a189cf07af714b2d1d3d4e64a38f79dbdbec Mon Sep 17 00:00:00 2001 From: Atinder Kumar <111070211+atinder11@users.noreply.github.com> Date: Tue, 4 Oct 2022 08:45:40 +0530 Subject: [PATCH 4/4] Add Average of levels in binary tree This program fixes issue #2 Question Link: https://leetcode.com/problems/average-of-levels-in-binary-tree/ I hope it is helpful and if any feedback or modifications please let me know. I hope you will merge it.