├── Week_3 ├── Linked List.pdf ├── Extra_questions.md ├── Linked List Cycle.cpp ├── Resources.md ├── Intersection of Two Linked Lists.cpp └── Odd Even Linked List.cpp ├── Syllabus.md ├── Week_1 ├── Presentation Link.md ├── Extra_questions.md ├── Two Sum.cpp ├── Squares of a Sorted Array.cpp ├── 3Sum └── Squares of a Sorted Array.py ├── Week_6 ├── Extra_questions.md ├── Resources.md ├── Find Center of Star Graph.cpp └── Number of Provinces.cpp ├── Week_9 ├── Extra_questions.md ├── Unique Paths.cpp ├── Longest Increasing Subsequence.cpp └── Resources.md ├── Week_2 ├── Extra_questions.md ├── Integer to Roman.cpp ├── Binary Search.cpp └── Add Strings.cpp ├── Week_4 ├── Resources.md ├── Extra_questions.md ├── Remove All Adjacent.cpp ├── Check If Word Is Valid After Substitutions.cpp └── Valid Parentheses.cpp ├── Week_7 ├── Extra_questions.md ├── Resources.md ├── Binary Tree Level Order Traversal.cpp └── Number of Islands.cpp ├── Week_5 ├── Resources.md ├── Symmetric Tree.cpp ├── Validate Binary Search Tree └── Extra_questions.md ├── Week_8 ├── Resources.md ├── Container With Most Water.cpp └── Sort an Array.cpp ├── complexity.md ├── Week_10 ├── Coin Change.py ├── Reverse String using recursion.cpp └── Best Time to Buy and Sell Stock.cpp └── sorting.md /Week_3/Linked List.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sk9245/LeetCode_Bootcamp_Spring2022/HEAD/Week_3/Linked List.pdf -------------------------------------------------------------------------------- /Syllabus.md: -------------------------------------------------------------------------------- 1 | https://docs.google.com/document/d/1Hd-OF6WC_rJ-9YeYxdFur-n_Haotwzbd30HHZTDh_aQ 2 | 3 | Please log in with NYU email id 4 | -------------------------------------------------------------------------------- /Week_1/Presentation Link.md: -------------------------------------------------------------------------------- 1 | https://docs.google.com/presentation/d/1J-L51WhgA0NDgr6AO--W3ubWOV8_f60UDE2IePGBm1E 2 | 3 | Log in with NYU email id 4 | -------------------------------------------------------------------------------- /Week_6/Extra_questions.md: -------------------------------------------------------------------------------- 1 | 1. https://leetcode.com/problems/find-if-path-exists-in-graph/ 2 | 2. https://leetcode.com/problems/find-the-town-judge/ 3 | 3. https://leetcode.com/problems/keys-and-rooms/ 4 | 4. https://leetcode.com/problems/regions-cut-by-slashes/ 5 | -------------------------------------------------------------------------------- /Week_9/Extra_questions.md: -------------------------------------------------------------------------------- 1 | 1. https://leetcode.com/problems/unique-paths-ii/ 2 | 2. https://leetcode.com/problems/minimum-path-sum/ 3 | 3. https://leetcode.com/problems/increasing-triplet-subsequence/ 4 | 4. https://leetcode.com/problems/maximum-length-of-pair-chain/ 5 | 5. https://leetcode.com/problems/number-of-longest-increasing-subsequence/ 6 | -------------------------------------------------------------------------------- /Week_2/Extra_questions.md: -------------------------------------------------------------------------------- 1 | 1. https://leetcode.com/problems/valid-palindrome/ 2 | 2. https://leetcode.com/problems/valid-palindrome-ii/ 3 | 3. https://leetcode.com/problems/valid-anagram/ 4 | 4. https://leetcode.com/problems/group-anagrams/ 5 | 5. https://leetcode.com/problems/roman-to-integer/ 6 | 6. https://leetcode.com/problems/first-unique-character-in-a-string/ 7 | -------------------------------------------------------------------------------- /Week_4/Resources.md: -------------------------------------------------------------------------------- 1 | 1. https://www.geeksforgeeks.org/stack-in-cpp-stl/ 2 | 2. https://www.geeksforgeeks.org/queue-cpp-stl/ 3 | 3. https://www.geeksforgeeks.org/difference-between-stack-and-queue-data-structures/ 4 | 4. https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm 5 | 5. https://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm 6 | -------------------------------------------------------------------------------- /Week_6/Resources.md: -------------------------------------------------------------------------------- 1 | 1. https://www.youtube.com/watch?v=gXgEDyodOJU 2 | 2. https://www.youtube.com/watch?v=tWVWeAqZ0WU 3 | 3. https://www.geeksforgeeks.org/graph-data-structure-and-algorithms/ 4 | 4. https://www.tutorialspoint.com/data_structures_algorithms/graph_data_structure.htm 5 | 5. https://en.wikipedia.org/wiki/Graph_(abstract_data_type) 6 | 6. https://www.educative.io/edpresso/what-is-a-graph-data-structure 7 | -------------------------------------------------------------------------------- /Week_7/Extra_questions.md: -------------------------------------------------------------------------------- 1 | 1. https://leetcode.com/problems/binary-tree-right-side-view/ 2 | 2. https://leetcode.com/problems/surrounded-regions/ 3 | 3. https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ 4 | 4. https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 5 | 5. https://leetcode.com/problems/binary-tree-maximum-path-sum/ 6 | 6. https://leetcode.com/problems/count-sub-islands/ 7 | -------------------------------------------------------------------------------- /Week_5/Resources.md: -------------------------------------------------------------------------------- 1 | 1. https://www.softwaretestinghelp.com/trees-in-cpp/ 2 | 2. https://www.geeksforgeeks.org/binary-tree-set-1-introduction/ 3 | 3. https://en.wikipedia.org/wiki/Tree_(data_structure)#:~:text=A%20tree%20data%20structure%20can,none%20points%20to%20the%20root. 4 | 4. https://www.geeksforgeeks.org/introduction-to-tree-data-structure/ 5 | 5. https://www.tutorialspoint.com/data_structures_algorithms/tree_data_structure.htm 6 | -------------------------------------------------------------------------------- /Week_3/Extra_questions.md: -------------------------------------------------------------------------------- 1 | 1. https://leetcode.com/problems/add-two-numbers/ 2 | 2. https://leetcode.com/problems/palindrome-linked-list/ 3 | 3. https://leetcode.com/problems/add-two-numbers-ii/ 4 | 4. https://leetcode.com/problems/odd-even-linked-list/ 5 | 5. https://leetcode.com/problems/intersection-of-two-linked-lists/ 6 | 6. https://leetcode.com/problems/merge-two-sorted-lists/ 7 | 7. https://leetcode.com/problems/reverse-linked-list/ 8 | -------------------------------------------------------------------------------- /Week_1/Extra_questions.md: -------------------------------------------------------------------------------- 1 | 1. https://leetcode.com/problems/merge-sorted-array/ 2 | 2. https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/ 3 | 3. https://leetcode.com/problems/3sum-closest/ 4 | 4. https://leetcode.com/problems/4sum/ 5 | 5. https://leetcode.com/problems/two-sum-less-than-k/ 6 | 6. https://leetcode.com/problems/kth-missing-positive-number/ 7 | 7. https://leetcode.com/problems/intersection-of-two-arrays/ 8 | -------------------------------------------------------------------------------- /Week_9/Unique Paths.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/unique-paths/ 2 | 3 | class Solution { 4 | public: 5 | int uniquePaths(int m, int n) { 6 | vector> grid(m, vector(n, 1)); 7 | 8 | for(int i = 1; i < m ; i++) 9 | for(int j = 1; j < n; j++) 10 | grid[i][j] = grid[i-1][j] + grid[i][j - 1]; 11 | 12 | return grid[m-1][n-1]; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Week_8/Resources.md: -------------------------------------------------------------------------------- 1 | 1. https://en.wikipedia.org/wiki/Divide-and-conquer_algorithm 2 | 2. https://www.geeksforgeeks.org/divide-and-conquer-algorithm-introduction/ 3 | 3. https://www.programiz.com/dsa/divide-and-conquer 4 | 4. https://www.tutorialspoint.com/data_structures_algorithms/divide_and_conquer.htm 5 | 5. https://en.wikipedia.org/wiki/Greedy_algorithm 6 | 6. https://www.hackerearth.com/practice/algorithms/greedy/basics-of-greedy-algorithms/tutorial/ 7 | -------------------------------------------------------------------------------- /complexity.md: -------------------------------------------------------------------------------- 1 | 1. https://www.bigocheatsheet.com/ 2 | 2. https://www.hackerearth.com/practice/basic-programming/complexity-analysis/time-and-space-complexity/tutorial/ 3 | 3. https://www.geeksforgeeks.org/understanding-time-complexity-simple-examples/ 4 | 4. https://www.geeksforgeeks.org/g-fact-86/ 5 | 5. https://www.geeksforgeeks.org/time-complexity-and-space-complexity/ 6 | 6. https://www.youtube.com/watch?v=D6xkbGLQesk 7 | 7. https://www.youtube.com/watch?v=v4cd1O4zkGw 8 | -------------------------------------------------------------------------------- /Week_10/Coin Change.py: -------------------------------------------------------------------------------- 1 | # https://leetcode.com/problems/coin-change/ 2 | 3 | class Solution(object): 4 | def coinChange(self, coins, amount): 5 | dp = [float('inf')] * (amount + 1) 6 | dp[0] = 0 7 | 8 | for coin in coins: 9 | for i in range(coin, amount + 1): 10 | dp[i] = min(dp[i], dp[i - coin] + 1) 11 | 12 | return dp[amount] if dp[amount] != float('inf') else -1 13 | 14 | -------------------------------------------------------------------------------- /Week_10/Reverse String using recursion.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/reverse-string/ 2 | 3 | class Solution { 4 | public: 5 | void ans(vector& s, int left, int right){ 6 | if(left>=right) 7 | return; 8 | swap(s[left], s[right]); 9 | left++; 10 | right--; 11 | ans(s, left, right); 12 | } 13 | 14 | void reverseString(vector& s) { 15 | ans(s, 0, s.size() - 1); 16 | 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Week_4/Extra_questions.md: -------------------------------------------------------------------------------- 1 | 1. https://leetcode.com/problems/132-pattern/ 2 | 2. https://leetcode.com/problems/add-two-numbers-ii/ 3 | 3. https://leetcode.com/problems/implement-queue-using-stacks/ 4 | 4. https://leetcode.com/problems/decode-string/ 5 | 5. https://leetcode.com/problems/clumsy-factorial/ 6 | 6. https://leetcode.com/problems/backspace-string-compare/ 7 | 7. https://leetcode.com/problems/implement-queue-using-stacks/ 8 | 8. https://leetcode.com/problems/implement-stack-using-queues/ 9 | -------------------------------------------------------------------------------- /Week_5/Symmetric Tree.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/symmetric-tree/ 2 | 3 | class Solution { 4 | public: 5 | bool helper(TreeNode* l, TreeNode* r) 6 | { 7 | if(!l || !r) return l == r; 8 | if(l->val != r->val) return false; 9 | 10 | bool left = helper(l->left,r->right); 11 | bool right = helper(l->right,r->left); 12 | 13 | return (left && right); 14 | } 15 | 16 | bool isSymmetric(TreeNode* root) { 17 | return helper(root->left,root->right); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Week_10/Best Time to Buy and Sell Stock.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 2 | 3 | class Solution { 4 | public: 5 | int maxProfit(vector& prices) { 6 | int minp = INT_MAX; 7 | int maxp = 0; 8 | for(int i = 0; i < prices.size(); i++){ 9 | if (prices[i] < minp) 10 | minp = prices[i]; 11 | else if(prices[i] - minp > maxp) 12 | maxp = prices[i] - minp; 13 | 14 | } 15 | return maxp; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Week_7/Resources.md: -------------------------------------------------------------------------------- 1 | 1. https://en.wikipedia.org/wiki/Breadth-first_search 2 | 2. https://www.geeksforgeeks.org/breadth-first-search-or-bfs-for-a-graph/ 3 | 3. https://www.tutorialspoint.com/data_structures_algorithms/breadth_first_traversal.htm 4 | 4. https://en.wikipedia.org/wiki/Depth-first_search 5 | 5. https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/ 6 | 6. https://www.tutorialspoint.com/data_structures_algorithms/depth_first_traversal.htm 7 | 7. https://www.geeksforgeeks.org/difference-between-bfs-and-dfs/ 8 | 8. https://www.educative.io/edpresso/dfs-vs-bfs 9 | -------------------------------------------------------------------------------- /Week_6/Find Center of Star Graph.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/find-center-of-star-graph/ 2 | 3 | class Solution { 4 | public: 5 | int findCenter(vector>& edges) { 6 | map mymap; 7 | for(int i=0;i next){ 12 | slow = slow->next; 13 | fast = fast->next->next; 14 | 15 | if(slow == fast) 16 | return true; 17 | } 18 | 19 | return false 20 | } 21 | }; 22 | 23 | -------------------------------------------------------------------------------- /Week_9/Longest Increasing Subsequence.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/longest-increasing-subsequence/submissions/ 2 | 3 | class Solution { 4 | public: 5 | int lengthOfLIS(vector& nums) { 6 | vector dp(nums.size(), 1); 7 | 8 | for(int i = 0; i < nums.size(); i++) 9 | { 10 | for(int j = 0; j < i; j++) 11 | { 12 | if(nums[i] > nums[j]) 13 | dp[i] = max(dp[i], dp[j] + 1); 14 | } 15 | } 16 | 17 | return *max_element(dp.begin(), dp.end()); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Week_5/Validate Binary Search Tree: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/validate-binary-search-tree/ 2 | 3 | class Solution { 4 | public: 5 | bool ans(TreeNode* root, TreeNode* low,TreeNode* high){ 6 | if(root == NULL) 7 | return true; 8 | if((low != NULL and root->val <= low->val) || (high != NULL and root->val >= high -> val)) 9 | return false; 10 | return ans(root->left, low, root) and ans(root->right, root, high); 11 | 12 | 13 | } 14 | bool isValidBST(TreeNode* root) { 15 | return ans(root, NULL, NULL); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Week_3/Resources.md: -------------------------------------------------------------------------------- 1 | ### Videos 2 | 1. https://www.youtube.com/watch?v=njTh_OwMljA 3 | 2. https://www.youtube.com/watch?v=WwfhLC16bis 4 | 3. https://www.youtube.com/watch?v=NobHlGUjV3g 5 | 6 | ### Reading 7 | 1. https://en.wikipedia.org/wiki/Linked_list#:~:text=In%20computer%20science%2C%20a%20linked,which%20together%20represent%20a%20sequence. 8 | 2. https://www.geeksforgeeks.org/data-structures/linked-list/ 9 | 3. https://www.tutorialspoint.com/data_structures_algorithms/linked_list_algorithms.htm 10 | 4. https://www.programiz.com/dsa/linked-list 11 | 5. https://www.interviewbit.com/courses/programming/topics/linked-lists/ 12 | -------------------------------------------------------------------------------- /Week_9/Resources.md: -------------------------------------------------------------------------------- 1 | 1. https://www.geeksforgeeks.org/dynamic-programming/ 2 | 2. https://www.educative.io/courses/grokking-dynamic-programming-patterns-for-coding-interviews/m2G1pAq0OO0 3 | 3. https://stackoverflow.com/questions/12133754/whats-the-difference-between-recursion-memoization-dynamic-programming#:~:text=Recursion%20happens%20whenever%20a%20function,This%20is%20all.&text=Dynamic%20programming%20is%20when%20you,terms%20of%20a%20recursive%20function. 4 | 4. https://www.programiz.com/dsa/dynamic-programming 5 | 5. https://www.hackerearth.com/practice/algorithms/dynamic-programming/introduction-to-dynamic-programming-1/tutorial/ 6 | -------------------------------------------------------------------------------- /Week_3/Intersection of Two Linked Lists.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/intersection-of-two-linked-lists/ 2 | 3 | class Solution { 4 | public: 5 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 6 | ListNode* a = headA; 7 | ListNode* b = headB; 8 | while(a != b){ 9 | if(a == NULL){ 10 | a = headB; 11 | } 12 | else{ 13 | a = a -> next; 14 | } 15 | if(b == NULL){ 16 | b = headA; 17 | } 18 | else{ 19 | b = b -> next; 20 | } 21 | } 22 | return a; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Week_1/Two Sum.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/two-sum/ 2 | 3 | class Solution { 4 | public: 5 | vector twoSum(vector& nums, int target) { 6 | unordered_map mp; 7 | vector ans; 8 | 9 | for(int i = 0; i nums = {1000,900,500,400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; 7 | string romans[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; 8 | string result = ""; 9 | int i = 0; 10 | while(num>0) 11 | { 12 | if(num>=nums[i]) 13 | { 14 | result += romans[i]; 15 | num = num - nums[i]; 16 | i = -1; 17 | } 18 | i++; 19 | } 20 | return result; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /sorting.md: -------------------------------------------------------------------------------- 1 | 1. https://visualgo.net/en/sorting?slide=1 2 | 2. https://www.tutorialspoint.com/data_structures_algorithms/sorting_algorithms.htm 3 | 3. https://www.geeksforgeeks.org/sorting-algorithms/ 4 | 5 | 6 | ### Sorting Algorithm 7 | 1. Insertion Sort - https://www.geeksforgeeks.org/insertion-sort/ 8 | 2. Selection Sort - https://www.geeksforgeeks.org/selection-sort/ 9 | 3. Bubble Sort - https://www.geeksforgeeks.org/bubble-sort/ 10 | 4. Merge Sort - https://www.geeksforgeeks.org/merge-sort/ - IMPORTANT 11 | 5. Quick Sort - https://www.geeksforgeeks.org/quick-sort/ - IMPORTANT 12 | 13 | Difference between Merge Sort and Quick Sort - https://www.geeksforgeeks.org/quick-sort-vs-merge-sort/ 14 | -------------------------------------------------------------------------------- /Week_5/Extra_questions.md: -------------------------------------------------------------------------------- 1 | 1. https://leetcode.com/problems/deepest-leaves-sum/ 2 | 2. https://leetcode.com/problems/range-sum-of-bst/ 3 | 3. https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/ 4 | 4. https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/ 5 | 5. https://leetcode.com/problems/merge-two-binary-trees/ 6 | 6. https://leetcode.com/problems/increasing-order-search-tree/ 7 | 7. https://leetcode.com/problems/n-ary-tree-preorder-traversal/ 8 | 8. https://leetcode.com/problems/search-in-a-binary-search-tree/ 9 | 9. https://leetcode.com/problems/count-good-nodes-in-binary-tree/ 10 | 10. https://leetcode.com/problems/maximum-depth-of-binary-tree/ 11 | -------------------------------------------------------------------------------- /Week_4/Remove All Adjacent.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/ 2 | 3 | class Solution { 4 | public: 5 | string removeDuplicates(string s) { 6 | string ans = ""; 7 | stack st; 8 | for(int i = 0; i < s.length(); i++){ 9 | if(st.empty()) 10 | st.push(s[i]); 11 | else if(s[i] == st.top()) 12 | st.pop(); 13 | else 14 | st.push(s[i]); 15 | } 16 | 17 | while(!st.empty()){ 18 | ans += st.top(); 19 | st.pop(); 20 | } 21 | 22 | reverse(ans.begin(), ans.end()); 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Week_1/Squares of a Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/squares-of-a-sorted-array/ 2 | 3 | class Solution { 4 | public: 5 | vector sortedSquares(vector& nums) { 6 | int n = nums.size(); 7 | vector ans(n); 8 | int left = 0; 9 | int right = n - 1; 10 | for(int i = n - 1; i >= 0; i--){ 11 | int temp; 12 | if(abs(nums[left]) next; 10 | ListNode* evenHead = even; 11 | while (even != NULL && even->next != NULL) { 12 | odd->next = even->next; 13 | odd = odd->next; 14 | even->next = odd->next; 15 | even = even->next; 16 | } 17 | odd->next = evenHead; 18 | return head; 19 | } 20 | }; 21 | 22 | // Time complexity : O(n)O(n). There are total nn nodes and we visit each node once. 23 | -------------------------------------------------------------------------------- /Week_2/Binary Search.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/binary-search/ 2 | 3 | class Solution { 4 | public: 5 | int search(vector& nums, int target) { 6 | int pivot, left = 0, right = nums.size() - 1; 7 | while (left <= right) { 8 | pivot = left + (right - left) / 2; 9 | if (nums[pivot] == target) return pivot; 10 | if (target < nums[pivot]) right = pivot - 1; 11 | else left = pivot + 1; 12 | } 13 | return -1; 14 | } 15 | }; 16 | 17 | /* 18 | Time complexity 19 | Length of array = n 20 | After first Iteration = n / 2 21 | After second Iteration = n / (2 * 2) 22 | .... 23 | After k Iteration = n / (2 ^ k) 24 | = 1 25 | 26 | n / (2 ^ k) = 1 27 | n = 2 ^ k 28 | log n = log (2 ^ k) 29 | log n = k log 2 30 | k = log n 31 | */ 32 | -------------------------------------------------------------------------------- /Week_8/Container With Most Water.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/container-with-most-water/ 2 | 3 | class Solution { 4 | public: 5 | int maxArea(vector& height) { 6 | int left=0, 7 | right=height.size()-1; 8 | //considering max product initially to be prod of first and last num 9 | int max_amount=INT_MIN; 10 | 11 | while(left=0 || len2 >= 0){ 11 | 12 | const int x = len1>=0 ? num1[len1] - '0': 0; 13 | const int y = len2>=0 ? num2[len2] - '0': 0; 14 | int z = x + y + carry; 15 | if(z >= 10){ 16 | z = z - 10; 17 | carry = 1; 18 | 19 | } 20 | 21 | else 22 | carry = 0; 23 | sum = to_string(z) + sum; 24 | len1--; 25 | len2--; 26 | 27 | } 28 | return sum; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Week_4/Check If Word Is Valid After Substitutions.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/ 2 | 3 | class Solution { 4 | public: 5 | bool isValid(string s) { 6 | stack st; 7 | 8 | for(int i = 0 ; i < s.size() ; i++){ 9 | char ch = s[i]; 10 | 11 | if(ch == 'a' || ch == 'b'){ 12 | st.push(ch); 13 | } 14 | else{ 15 | if(st.size() >= 2){ 16 | char x = st.top(); 17 | st.pop(); 18 | char y = st.top(); 19 | st.pop(); 20 | if(x != 'b' || y != 'a') 21 | return false; 22 | } 23 | else{ 24 | return false; 25 | } 26 | } 27 | } 28 | 29 | return st.size() == 0; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Week_6/Number of Provinces.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/number-of-provinces/ 2 | 3 | class Solution { 4 | public: 5 | int findCircleNum(vector>& isConnected) { 6 | int noOfProvinces = 0; 7 | int n = isConnected.size(); 8 | vector visited(n,false); 9 | for(int i=0;i& visited, int current, vector> isConnected) { 20 | if(visited[current] == true) { 21 | return; 22 | } 23 | visited[current] = true; 24 | for(int i=0;i> levelOrder(TreeNode* root) { 6 | 7 | vector> ans; 8 | 9 | if(root==nullptr) return ans; 10 | vector level; 11 | queue q; 12 | 13 | q.push(root); 14 | TreeNode *node; 15 | while(!q.empty()){ 16 | vector level; 17 | int size=q.size(); 18 | 19 | for(int i=0; ival); 23 | if(node->left!=nullptr){ 24 | q.push(node->left); 25 | } 26 | if(node->right!=nullptr){ 27 | q.push(node->right); 28 | } 29 | } 30 | 31 | ans.push_back(level); 32 | 33 | } 34 | return ans; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /Week_1/3Sum: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/3sum/ 2 | 3 | class Solution { 4 | public: 5 | vector> threeSum(vector& nums) { 6 | set> res; 7 | unordered_set dups; 8 | unordered_map seen; 9 | 10 | for(int i = 0; i < nums.size(); i++){ // O(n) 11 | if(dups.insert(nums[i]).second){ 12 | for(int j = i+1; j < nums.size(); ++j){ // o(n) 13 | int temp = - nums[i] - nums[j]; 14 | auto it = seen.find(temp); 15 | if(it != end(seen) && it->second == i){ 16 | vector triplet = {nums[i], nums[j], temp}; 17 | // -1, 2, -1 18 | // -1, -1, 2 19 | 20 | sort(begin(triplet), end(triplet)); 21 | res.insert(triplet); 22 | } 23 | seen[nums[j]] = i; 24 | } 25 | } 26 | } 27 | return vector>(begin(res), end(res)); 28 | } 29 | }; 30 | 31 | -------------------------------------------------------------------------------- /Week_8/Sort an Array.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/sort-an-array/ 2 | 3 | class Solution { 4 | public: 5 | vector sortArray(vector& nums) { 6 | mergeSort(nums, 0, nums.size() - 1); 7 | return nums; 8 | } 9 | 10 | void mergeSort(vector &nums, int l, int r) { 11 | if(l < r) { 12 | int mid = (l + r) / 2; 13 | mergeSort(nums, l, mid); 14 | mergeSort(nums, mid + 1, r); 15 | merge(nums, l, mid, r); 16 | } 17 | } 18 | void merge(vector &nums, int l, int mid, int r) { 19 | int n1 = mid - l + 1; 20 | int n2 = r - mid; 21 | 22 | int a[n1]; 23 | int b[n2]; 24 | 25 | int i = 0, j = 0, k = l; 26 | 27 | for(int i = 0; i < n1; i++) { 28 | a[i] = nums[i + l]; 29 | } 30 | 31 | for(int j = 0; j < n2; j++) { 32 | b[j] = nums[mid + 1 + j]; 33 | } 34 | 35 | while(i < n1 && j < n2) { 36 | if(a[i] <= b[j]) nums[k++] = a[i++]; 37 | else nums[k++] = b[j++]; 38 | } 39 | while(i < n1) 40 | nums[k++] = a[i++]; 41 | 42 | while(j < n2) 43 | nums[k++] = b[j++]; 44 | } 45 | }; 46 | -------------------------------------------------------------------------------- /Week_7/Number of Islands.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/number-of-islands/ 2 | 3 | class Solution { 4 | public: 5 | void dfs(vector>& grid, int row, int col){ 6 | int r = grid.size(); 7 | int c = grid[0].size(); 8 | 9 | grid[row][col] = '0'; 10 | 11 | if(row - 1 >= 0 && grid[row-1][col] == '1') dfs(grid, row - 1, col); 12 | if(row + 1 < r && grid[row + 1][col] == '1') dfs(grid, row + 1, col); 13 | if(col - 1 >= 0 && grid[row][col - 1] == '1') dfs(grid, row, col - 1); 14 | if(col + 1 < c && grid[row][col + 1] == '1') dfs(grid, row, col+ 1); 15 | } 16 | 17 | int numIslands(vector>& grid) { 18 | int row = grid.size(); 19 | int col = grid[0].size(); 20 | 21 | if (row == 0) 22 | return 0; 23 | 24 | int count = 0; 25 | 26 | for(int i = 0; i < row; i++){ 27 | for(int j = 0; j < col; j++){ 28 | if(grid[i][j] == '1'){ 29 | count++; 30 | dfs(grid, i, j); 31 | } 32 | } 33 | } 34 | 35 | return count; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /Week_4/Valid Parentheses.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/valid-parentheses/ 2 | 3 | class Solution { 4 | public: 5 | bool isValid(string s) { 6 | stack st; 7 | for(int i = 0; i < s.length() ; i++){ 8 | if(s[i] == '(' || s[i] == '{' || s[i] == '[') 9 | st.push(s[i]); 10 | else { 11 | if(st.empty()) 12 | return false; 13 | char t = st.top(); 14 | if (s[i] == ')'){ 15 | if (t != '(') 16 | return false; 17 | else 18 | st.pop(); 19 | } 20 | else if (s[i] == '}'){ 21 | if (t != '{') 22 | return false; 23 | else 24 | st.pop(); 25 | } else if (s[i] == ']'){ 26 | if (t != '[') 27 | return false; 28 | else 29 | st.pop(); 30 | } 31 | } 32 | } 33 | 34 | if (st.empty()) 35 | return true; 36 | return false; 37 | } 38 | }; 39 | 40 | // O(n) 41 | -------------------------------------------------------------------------------- /Week_1/Squares of a Sorted Array.py: -------------------------------------------------------------------------------- 1 | # https://leetcode.com/problems/squares-of-a-sorted-array/ 2 | 3 | class Solution: 4 | def sortedSquares(self, nums: List[int]) -> List[int]: 5 | array_size = len(nums) # save initial length 6 | output_array = [0] * array_size # output array makes this space complexity = O(N) 7 | left = 0 # left pointer starts at the left side of the array 8 | right = array_size - 1 # right pointer starts at the right end 9 | output_counter = array_size - 1 # need output counter to keep track of where to place squares in the output array 10 | while left <= right: # while both pointers are NOT crossing (can be the same so the last "middle" element gets added (not always necessarily going to be in the center of the array unless array is balanced) 11 | square_left = nums[left] ** 2 12 | square_right = nums[right] ** 2 13 | if square_left > square_right: 14 | output_array[output_counter] = square_left 15 | left += 1 # increment left side to narrow search width 16 | else: 17 | output_array[output_counter] = square_right 18 | right -= 1 # decrement right side to narrow search width 19 | output_counter -= 1 # decrement output counter to show where the next element will be inserted in the output array 20 | 21 | # returns a new array 22 | # SC: O(N) TC: O(N) 23 | return output_array 24 | --------------------------------------------------------------------------------