Skip to content

Commit 686c6b1

Browse files
authored
Merge branch 'neetcode-gh:main' into main
2 parents 48f6c9a + 68a636a commit 686c6b1

File tree

56 files changed

+1380
-2
lines changed

Some content is hidden

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

56 files changed

+1380
-2
lines changed

cpp/1-Two-Sum.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target) {
4+
unordered_map<int, int> umap;
5+
for(int i=0; i<nums.size(); i++) {
6+
if (umap.find(target-nums[i]) != umap.end()){
7+
return {umap[target-nums[i]], i};
8+
}
9+
10+
umap[nums[i]] = i;
11+
}
12+
13+
return {};
14+
}
15+
};

cpp/100-Same-Tree.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
bool isSameTree(TreeNode* p, TreeNode* q) {
15+
if (p == NULL and q == NULL) return true;
16+
if (p==NULL || q==NULL) return false;
17+
if (p->val != q->val) return false;
18+
19+
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
20+
}
21+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
13+
// Using Stack
14+
// class Solution {
15+
// public:
16+
// int maxDepth(TreeNode* root) {
17+
// if (root == NULL) return 0;
18+
// stack<pair<TreeNode*, int>> stk;
19+
// int res = 0;
20+
// stk.push({root, 1});
21+
22+
// while (!stk.empty()) {
23+
// auto t = stk.top();
24+
// res = max(res, t.second);
25+
// stk.pop();
26+
// if (t.first->left) stk.push({t.first->left, t.second+1});
27+
// if (t.first->right) stk.push({t.first->right, t.second+1});
28+
// }
29+
// return res;
30+
// }
31+
// };
32+
33+
// Using Recursion
34+
class Solution {
35+
public:
36+
int maxDepth(TreeNode* root) {
37+
if (root == NULL) return 0;
38+
return max(maxDepth(root->left)+1, maxDepth(root->right)+1);
39+
}
40+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices) {
4+
int n = prices.size();
5+
// Preprocessing
6+
// Polulate a smallest from beginning array
7+
// Polulate a largest from the end array
8+
vector<int> smallest(n, -1), largest(n, -1);
9+
10+
smallest[0] = prices[0];
11+
largest[n-1] = prices[n-1];
12+
13+
for (int i = 1; i < n; i++) {
14+
smallest[i] = min(smallest[i-1], prices[i]);
15+
}
16+
17+
for (int i = n-2; i>=0; i--) {
18+
largest[i] = max(largest[i+1], prices[i]);
19+
}
20+
21+
// Logic
22+
// Max difference b/w the smallest and largest element
23+
int res = 0;
24+
for(int i=0; i<n; i++) res = max(res, largest[i]-smallest[i]);
25+
26+
return res;
27+
28+
}
29+
};

cpp/125-Valid-Palindrome.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
bool isPalindrome(string s) {
4+
int start = 0, end = s.length()-1;
5+
6+
while (start < end) {
7+
char a = s[start], b = s[end];
8+
9+
// Convert to caps
10+
if (a >= 'a' && a <= 'z') a = a-'a'+'A';
11+
if (b >= 'a' && b <= 'z') b = b - 'a' + 'A';
12+
13+
// Logic
14+
if ( (a < 'A' || a > 'Z') && (a < '0' || a > '9') ) {
15+
start ++;
16+
continue;
17+
}
18+
if ( (b < 'A' || b > 'Z') && (b < '0' || b > '9') ) {
19+
end --;
20+
continue;
21+
}
22+
if(a != b) return false;
23+
start ++;
24+
end --;
25+
}
26+
27+
return true;
28+
}
29+
};

cpp/143-Reorder-List.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
void reorderList(ListNode* head) {
14+
ListNode* prev = nullptr;
15+
ListNode* slow = head;
16+
ListNode* fast = head;
17+
18+
// set slow to middle node
19+
while (fast != nullptr && fast->next != nullptr) {
20+
prev = slow;
21+
slow = slow->next;
22+
fast = fast->next->next;
23+
}
24+
25+
if (prev != nullptr) {
26+
prev->next = reverseList(slow);
27+
mergeTwoLists(head, prev->next);
28+
}
29+
}
30+
31+
private:
32+
ListNode* reverseList(ListNode* head) {
33+
ListNode* prev = nullptr;
34+
ListNode* curr = head;
35+
36+
while (curr != nullptr) {
37+
ListNode* next = curr->next;
38+
curr->next = prev;
39+
prev = curr;
40+
curr = next;
41+
}
42+
43+
return prev;
44+
}
45+
46+
// invariant: list2 length = [list1 length, list1 length + 1]
47+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
48+
ListNode dummy;
49+
ListNode* prev = &dummy;
50+
51+
ListNode* l1 = list1;
52+
ListNode* l2 = list2;
53+
54+
while (l1 != list2) {
55+
takeNode(prev, l1);
56+
takeNode(prev, l2);
57+
}
58+
59+
// handle odd number of nodes
60+
if (l2 != nullptr) {
61+
takeNode(prev, l2);
62+
}
63+
64+
return dummy.next;
65+
}
66+
67+
void takeNode(ListNode*& prev, ListNode*& curr) {
68+
prev->next = curr;
69+
prev = prev->next;
70+
curr = curr->next;
71+
}
72+
};

cpp/167-Two-Sum-II.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Window Method
2+
3+
class Solution {
4+
private:
5+
6+
public:
7+
vector<int> twoSum(vector<int>& arr, int target) {
8+
int i = 0, j = arr.size()-1;
9+
10+
while(i < j){
11+
if(arr[i] + arr[j] == target) return {i+1,j+1};
12+
if(arr[i] + arr[j] > target) {
13+
j--;
14+
}
15+
else {i++;}
16+
}
17+
return {};
18+
}
19+
20+
};

cpp/20-Valid-Parentheses.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
bool isValid(string s) {
4+
stack<char> stk;
5+
for(auto a: s) {
6+
if (a=='(' || a=='{' || a=='[') {
7+
stk.push(a);
8+
}
9+
else {
10+
if(stk.empty()) return false;
11+
if( (stk.top() == '(' && a==')') || (stk.top() == '{' && a=='}') || (stk.top() == '[' && a==']') )
12+
stk.pop();
13+
else return false;
14+
}
15+
}
16+
17+
return stk.empty();
18+
}
19+
};

cpp/206-Reverse-Linked-List.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* reverseList(ListNode* head) {
14+
ListNode* rev = new ListNode(0);
15+
ListNode* ptr = head;
16+
17+
while(ptr) {
18+
auto temp = ptr;
19+
ptr=ptr->next;
20+
temp-> next = rev->next;
21+
rev->next = temp;
22+
}
23+
24+
return rev->next;
25+
}
26+
};

cpp/21-Merge-Two-Sorted-Lists.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
14+
ListNode* res = new ListNode(0);
15+
auto ptr = res;
16+
17+
auto ptr1 = list1;
18+
auto ptr2 = list2;
19+
20+
while (ptr1 && ptr2) {
21+
if (ptr1->val < ptr2->val) {
22+
res->next = ptr1;
23+
ptr1 = ptr1->next;
24+
res = res->next;
25+
}
26+
else {
27+
res->next = ptr2;
28+
ptr2 = ptr2->next;
29+
res = res->next;
30+
}
31+
}
32+
33+
while (ptr1) {
34+
res->next = ptr1;
35+
res = res->next;
36+
ptr1 = ptr1->next;
37+
}
38+
while (ptr2) {
39+
res->next = ptr2;
40+
res = res->next;
41+
ptr2 = ptr2->next;
42+
}
43+
44+
return ptr->next;
45+
}
46+
};

0 commit comments

Comments
 (0)