From 830bb4c8644dbc6d56393b0ba60960262b4f3060 Mon Sep 17 00:00:00 2001 From: Mudit Choraria Date: Tue, 6 Dec 2022 20:21:51 +0530 Subject: [PATCH 1/9] muditchoraria: add soln for odd even linked list --- Mudit Choraria/odd-even-ll.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Mudit Choraria/odd-even-ll.cpp diff --git a/Mudit Choraria/odd-even-ll.cpp b/Mudit Choraria/odd-even-ll.cpp new file mode 100644 index 00000000..a4f10bc2 --- /dev/null +++ b/Mudit Choraria/odd-even-ll.cpp @@ -0,0 +1,29 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* oddEvenList(ListNode* head) { + if(!head || !head->next || !head->next->next) return head; + + ListNode *odd = head; + ListNode *even = head->next; + ListNode *even_start = head->next; + + while(odd->next && even->next){ + odd->next = even->next; //Connect all odds + odd = odd->next; + even->next = odd->next; //Connect all evens + even = even->next; + } + odd->next = even_start; //Place the first even node after the last odd node. + return head; + } +}; \ No newline at end of file From 7bd6f414de2c1c5d17bbba3e473b74bf4e904257 Mon Sep 17 00:00:00 2001 From: Mudit Choraria Date: Wed, 7 Dec 2022 20:10:35 +0530 Subject: [PATCH 2/9] muditchoraria: add soln for range sum bst --- Mudit Choraria/range-sum-bst.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Mudit Choraria/range-sum-bst.cpp diff --git a/Mudit Choraria/range-sum-bst.cpp b/Mudit Choraria/range-sum-bst.cpp new file mode 100644 index 00000000..1c25e24f --- /dev/null +++ b/Mudit Choraria/range-sum-bst.cpp @@ -0,0 +1,26 @@ +/** + * 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: + int rangeSumBST(TreeNode* root, int low, int high) { + if(root == nullptr) { + return 0; + } + if(root->val < low) { + return rangeSumBST(root->right, low, high); + } + if(root->val > high) { + return rangeSumBST(root->left, low, high); + } + return root->val + rangeSumBST(root->left, low, high) + rangeSumBST(root->right, low, high); + } +}; \ No newline at end of file From accb72d94fdae75c2566615e7d16ec7e4df898ea Mon Sep 17 00:00:00 2001 From: Mudit Choraria Date: Thu, 8 Dec 2022 22:31:06 +0530 Subject: [PATCH 3/9] muditchoraria: add soln for leaf similar tree --- Mudit Choraria/leaf-similar-tree.cpp | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Mudit Choraria/leaf-similar-tree.cpp diff --git a/Mudit Choraria/leaf-similar-tree.cpp b/Mudit Choraria/leaf-similar-tree.cpp new file mode 100644 index 00000000..92d8746e --- /dev/null +++ b/Mudit Choraria/leaf-similar-tree.cpp @@ -0,0 +1,31 @@ +/** + * 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: + void tr(TreeNode* root, vector& l) { + if(root == nullptr) { + return; + } + if(root->left == nullptr && root->right == nullptr) { + l.push_back(root->val); + return; + } + tr(root->left, l); + tr(root->right, l); + } + bool leafSimilar(TreeNode* root1, TreeNode* root2) { + vector l1, l2; + tr(root1, l1); + tr(root2, l2); + return l1 == l2; + } +}; \ No newline at end of file From c98e7990f72a63e7ec3a6c92e52d6ad1c5cdcd56 Mon Sep 17 00:00:00 2001 From: Mudit Choraria Date: Fri, 9 Dec 2022 20:48:09 +0530 Subject: [PATCH 4/9] muditchoraria: add soln for max anc diff --- Mudit Choraria/max-anc-tree.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Mudit Choraria/max-anc-tree.cpp diff --git a/Mudit Choraria/max-anc-tree.cpp b/Mudit Choraria/max-anc-tree.cpp new file mode 100644 index 00000000..79dffadc --- /dev/null +++ b/Mudit Choraria/max-anc-tree.cpp @@ -0,0 +1,30 @@ +/** + * 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: + int ans = 0; + void maxAnc(TreeNode* root, int currMax, int currMin) { + if(root == nullptr) { + return; + } + ans = max(ans, abs(currMax - root->val)); + ans = max(ans, abs(currMin - root->val)); + currMax = max(currMax, root->val); + currMin = min(currMin, root->val); + maxAnc(root->left, currMax, currMin); + maxAnc(root->right, currMax, currMin); + } + int maxAncestorDiff(TreeNode* root) { + maxAnc(root, root->val, root->val); + return ans; + } +}; \ No newline at end of file From f8bafe4d67c86b495782d2cb389c1c3f7340fdca Mon Sep 17 00:00:00 2001 From: Mudit Choraria Date: Sat, 10 Dec 2022 16:17:19 +0530 Subject: [PATCH 5/9] muditchoraria: add soln for max prod binary tree split --- Mudit Choraria/max-prod-binary-tree-split.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Mudit Choraria/max-prod-binary-tree-split.cpp diff --git a/Mudit Choraria/max-prod-binary-tree-split.cpp b/Mudit Choraria/max-prod-binary-tree-split.cpp new file mode 100644 index 00000000..28902270 --- /dev/null +++ b/Mudit Choraria/max-prod-binary-tree-split.cpp @@ -0,0 +1,34 @@ +/** + * 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 allSums; + long ans = 0; + long MOD = 1e9 + 7; + long sum(TreeNode* root) { + if(root == nullptr) { + return 0; + } + long left = sum(root->left); + long right = sum(root->right); + allSums.push_back(root->val + left + right); + return allSums.back(); + } + int maxProduct(TreeNode* root) { + long totalSum = sum(root); + for (const long sum : allSums) { + ans = max(ans, sum * (totalSum - sum)); + } + + return ans % MOD; + } +}; \ No newline at end of file From c1f9df8df1fef860dff7c9e329f7b73f40863797 Mon Sep 17 00:00:00 2001 From: Mudit Choraria Date: Sun, 11 Dec 2022 17:59:46 +0530 Subject: [PATCH 6/9] muditchoraria: add soln for binary tree max path sum --- .../binary-tree-max-prod-path-sum.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Mudit Choraria/binary-tree-max-prod-path-sum.cpp diff --git a/Mudit Choraria/binary-tree-max-prod-path-sum.cpp b/Mudit Choraria/binary-tree-max-prod-path-sum.cpp new file mode 100644 index 00000000..a9e8a47b --- /dev/null +++ b/Mudit Choraria/binary-tree-max-prod-path-sum.cpp @@ -0,0 +1,30 @@ +/** + * 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: + int ans = INT_MIN; + int maxPathSum(TreeNode* root) { + maxSumDown(root); + return ans; + } +private: + int maxSumDown(TreeNode* root) { + if(root == nullptr) { + return 0; + } + + int leftPathSum = max(0, maxSumDown(root->left)); + int rightPathSum = max(0, maxSumDown(root->right)); + ans = max(ans, root->val + leftPathSum + rightPathSum); + return root->val + max(leftPathSum, rightPathSum); + } +}; \ No newline at end of file From 04022658120490fc6de67e8e8ec7fac78a1588f8 Mon Sep 17 00:00:00 2001 From: Mudit Choraria Date: Mon, 12 Dec 2022 22:39:19 +0530 Subject: [PATCH 7/9] muditchoraria: add soln for reorder routes --- Mudit Choraria/reorder-routes.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Mudit Choraria/reorder-routes.cpp diff --git a/Mudit Choraria/reorder-routes.cpp b/Mudit Choraria/reorder-routes.cpp new file mode 100644 index 00000000..557cdd25 --- /dev/null +++ b/Mudit Choraria/reorder-routes.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int minReorder(int n, vector>& connections) { + vector adj[n + 5]; + for(auto con: connections) { + adj[con[0]].push_back(con[1]); + adj[con[1]].push_back(-con[0]); + } + vector vis(n + 5, false); + queue q; + q.push(0); + vis[0] = true; + int ans = 0; + while(!q.empty()) { + int curr = q.front(); + q.pop(); + + for(auto i: adj[curr]) { + if(!vis[abs(i)]) { + if(i > 0) { + ans++; + } + vis[abs(i)] = true; + q.push(abs(i)); + } + } + } + return ans; + } +}; \ No newline at end of file From f478ca5191397989b5f7685dd055b0b076a95bd6 Mon Sep 17 00:00:00 2001 From: Mudit Choraria Date: Tue, 13 Dec 2022 21:43:57 +0530 Subject: [PATCH 8/9] muditchoraria: add soln for min falling path sum --- Mudit Choraria/min-falling-path-sum.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Mudit Choraria/min-falling-path-sum.cpp diff --git a/Mudit Choraria/min-falling-path-sum.cpp b/Mudit Choraria/min-falling-path-sum.cpp new file mode 100644 index 00000000..1fb5ca2f --- /dev/null +++ b/Mudit Choraria/min-falling-path-sum.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int minFallingPathSum(vector>& matrix) { + int n = matrix.size(); + int m = matrix[0].size(); + int ans = INT_MAX; + for(int i = 1; i < m; i++) { + for(int j = 0; j < n; j++) { + if(j == 0 && j + 1 < n) { + matrix[i][j] += min(matrix[i - 1][j], matrix[i - 1][j + 1]); + } else if(j == n - 1 && j - 1 >= 0) { + matrix[i][j] += min(matrix[i - 1][j], matrix[i - 1][j - 1]); + } else { + matrix[i][j] += min(min(matrix[i - 1][j], matrix[i - 1][j - 1]), matrix[i - 1][j + 1]); + } + } + } + for(auto i: matrix[m - 1]) { + ans = min(ans, i); + } + return ans; + } +}; \ No newline at end of file From 8774dbe6e8a53d1df1115ae27e3e48ae78ec6a75 Mon Sep 17 00:00:00 2001 From: Mudit Choraria Date: Wed, 14 Dec 2022 20:26:17 +0530 Subject: [PATCH 9/9] muditchoraria: add soln for house robber --- Mudit Choraria/house-robber.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Mudit Choraria/house-robber.cpp diff --git a/Mudit Choraria/house-robber.cpp b/Mudit Choraria/house-robber.cpp new file mode 100644 index 00000000..a89ccaae --- /dev/null +++ b/Mudit Choraria/house-robber.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int rob(vector& nums) { + int pre = 0, curr = 0; + for(int i = 0; i < nums.size(); i++) { + int tmp = max(pre + nums[i], curr); + pre = curr; + curr = tmp; + } + return curr; + } +}; \ No newline at end of file