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 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 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 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 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 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 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 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 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