Skip to content
Open
30 changes: 30 additions & 0 deletions Mudit Choraria/binary-tree-max-prod-path-sum.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
};
12 changes: 12 additions & 0 deletions Mudit Choraria/house-robber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
int rob(vector<int>& 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;
}
};
31 changes: 31 additions & 0 deletions Mudit Choraria/leaf-similar-tree.cpp
Original file line number Diff line number Diff line change
@@ -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<int>& 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<int> l1, l2;
tr(root1, l1);
tr(root2, l2);
return l1 == l2;
}
};
30 changes: 30 additions & 0 deletions Mudit Choraria/max-anc-tree.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};
34 changes: 34 additions & 0 deletions Mudit Choraria/max-prod-binary-tree-split.cpp
Original file line number Diff line number Diff line change
@@ -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<int> 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;
}
};
23 changes: 23 additions & 0 deletions Mudit Choraria/min-falling-path-sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int minFallingPathSum(vector<vector<int>>& 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;
}
};
29 changes: 29 additions & 0 deletions Mudit Choraria/odd-even-ll.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};
26 changes: 26 additions & 0 deletions Mudit Choraria/range-sum-bst.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
};
30 changes: 30 additions & 0 deletions Mudit Choraria/reorder-routes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public:
int minReorder(int n, vector<vector<int>>& connections) {
vector<int> adj[n + 5];
for(auto con: connections) {
adj[con[0]].push_back(con[1]);
adj[con[1]].push_back(-con[0]);
}
vector<bool> vis(n + 5, false);
queue<int> 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;
}
};