Skip to content

Commit c3ac112

Browse files
committed
ALL DSA Problems
1 parent 456ecd7 commit c3ac112

File tree

272 files changed

+6255
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

272 files changed

+6255
-4
lines changed

Leetcode_Practice_Questions

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// we will be writing the code for two sum (Leetcode #1)
2+
vector<int> twoSum(vector<int>& nums, int target) {
3+
int n = nums.size();
4+
unordered_map<int,int> hashmap;
5+
for(int i = 0; i<n;i++){
6+
int diff = target - nums[i];
7+
if(hashmap.find(diff) != hashmap.end()){
8+
return {hashmap[diff],i};
9+
}
10+
hashmap[nums[i]] = i;
11+
}
12+
return{};
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// we will be writing the code for Same Tree (Leetcode #100)
2+
bool isSameTree(TreeNode* p, TreeNode* q) {
3+
if(p==NULL && q==NULL){
4+
return true;
5+
}
6+
else if(p ==NULL && q!= NULL){
7+
return false;
8+
}
9+
else if (p!= NULL && q==NULL){
10+
return false;
11+
}
12+
else if(p->val != q-> val){
13+
return false;
14+
}
15+
else{
16+
return isSameTree(p->left,q->left) && isSameTree(p->right, q->right);
17+
}
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// we will be writing the code for Construct Binary Search Tree from Preorder Traversal (Leetcode # 1008)
2+
TreeNode* bstFromPreorder(vector<int>& preorder) {
3+
return build_tree(preorder, 0, preorder.size() -1);
4+
}
5+
TreeNode* build_tree(vector<int>& preorder, int l, int r){
6+
if(l>r){
7+
return nullptr;
8+
}
9+
TreeNode* root = new TreeNode(preorder[l]);
10+
if(l == r){
11+
return root;
12+
}
13+
int idx = l+1;
14+
while(idx <= r && preorder[idx] < preorder[l]){
15+
idx++;
16+
}
17+
root->left = build_tree(preorder,l+1,idx-1);
18+
root->right = build_tree(preorder,idx,r);
19+
return root;
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// we wil be writing the code for Binary Tree Level Order Traversal (LeetCode #102)
2+
vector<vector<int>> levelOrder(TreeNode* root) {
3+
if(root == nullptr){
4+
return vector<vector<int>>();
5+
}
6+
queue<TreeNode*>q;
7+
q.push(root);
8+
vector<vector<int>>result;
9+
while(!q.empty()){
10+
vector<int>level;
11+
int size = q.size();
12+
for(int i = 0; i < size; i++){
13+
TreeNode* Node = q.front();
14+
q.pop();
15+
level.push_back(Node->val);
16+
if(Node->left){
17+
q.push(Node->left);
18+
}
19+
if(Node->right){
20+
q.push(Node->right);
21+
}
22+
}
23+
result.push_back(level);
24+
}
25+
return result;
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// we will be writing the code for maximum difference between Node and Ancestor (Leetcode #1026)
2+
int maxi = 0;
3+
pair<int,int> ok(TreeNode* root){
4+
if(root == NULL) return {0,0};
5+
auto l = (root->left==NULL ? make_pair(0,0): ok(root->left));
6+
auto r = (root->right==NULL ? make_pair(0,0): ok(root->right));
7+
if(root->left == NULL && root->right==NULL) return {root->val,root->val};
8+
int curr_maxi = root->val;
9+
int curr_mini = root->val;
10+
if(root->left!= NULL){
11+
maxi = max({maxi,abs(root->val - l.first),abs(root->val - l.second)});
12+
curr_maxi = max(curr_maxi,l.second);
13+
curr_mini = min(curr_mini,l.first);
14+
}
15+
if(root->right!=NULL){
16+
maxi= max({maxi,abs(root->val - r.first), abs(root->val - r.second)});
17+
curr_maxi = max(curr_maxi,r.second);
18+
curr_mini = min(curr_mini,r.first);
19+
}
20+
return {curr_mini,curr_maxi};
21+
}
22+
int maxAncestorDiff(TreeNode* root) {
23+
ok(root);
24+
return maxi;
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// we will be writing the code for Remove All Adjacent Duplicates in String (Leetcode #1047)
2+
string removeDuplicates(string s) {
3+
string ans = "";
4+
int i = 0;
5+
while(i<s.length()){
6+
if(ans.length() > 0){
7+
if(ans[ans.length() - 1] == s[i]) {
8+
ans.pop_back();
9+
}
10+
else{
11+
ans.push_back(s[i]);
12+
}
13+
}
14+
else{
15+
ans.push_back(s[i]);
16+
}
17+
i++;
18+
}
19+
return ans;
20+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// we will be writing the code for Container with Most Water(Leetcode #11)
2+
int maxArea(vector<int>& height) {
3+
int min_height = height[0];
4+
int max_height = height[height.size()-1];
5+
int min_idx = 0;
6+
int max_idx = height.size()-1;
7+
if(max_height < min_height){
8+
min_height = max_height;
9+
min_idx = max_idx;
10+
max_height = height[0];
11+
max_idx = 0;
12+
}
13+
int w = height.size() - 1;
14+
int max_area = min_height * w;
15+
while(w > 0){
16+
int idx = min_idx;
17+
while(height[idx] <= min_height){
18+
if(min_idx < max_idx){
19+
idx++;
20+
}
21+
else{
22+
idx--;
23+
}
24+
w--;
25+
if(w<1){
26+
break;
27+
}
28+
}
29+
if(height[idx] > max_height){
30+
min_height = max_height;
31+
min_idx = max_idx;
32+
max_idx = idx;
33+
max_height = height[idx];
34+
}
35+
else{
36+
min_idx = idx;
37+
min_height = height[idx];
38+
}
39+
max_area = max(max_area, w*min_height);
40+
}
41+
return max_area;
42+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// we will be writing the code for Balanced Binary Tree (Leetcode #110)
2+
bool isBalanced(TreeNode* root) {
3+
bool ans = true;
4+
checkBalance(root,ans);
5+
return ans;
6+
}
7+
int checkBalance(TreeNode* root, bool &ans){
8+
if(root==NULL){
9+
return 0;
10+
}
11+
if(root->left == NULL && root->right== NULL){
12+
return 1;
13+
}
14+
int left = checkBalance(root->left,ans);
15+
int right = checkBalance(root->right,ans);
16+
if(abs(left - right)>1){
17+
ans = false;
18+
}
19+
return left>right ? left+1 : right + 1;
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// we will be writing the code for Minimum Depth of Binary Tree (Leetcode #111)
2+
int minDepth(TreeNode* root) {
3+
if(root == NULL){
4+
return 0;
5+
}
6+
if(root->left == NULL && root->right == NULL){
7+
return 1;
8+
}
9+
int left = minDepth(root->left);
10+
int right = minDepth(root->right);
11+
if(left == 0){
12+
return right + 1;
13+
}
14+
if(right == 0){
15+
return left + 1;
16+
}
17+
return left<right ? left + 1: right+1;
18+
}

0 commit comments

Comments
 (0)