From 6149109057796b18f6d0f17cfd5c167edc1d22e7 Mon Sep 17 00:00:00 2001 From: Simran-Sahni Date: Fri, 8 Oct 2021 12:51:24 +0530 Subject: [PATCH 1/4] House Robber 3 question --- C++/House-Robber-3.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 C++/House-Robber-3.cpp diff --git a/C++/House-Robber-3.cpp b/C++/House-Robber-3.cpp new file mode 100644 index 00000000..c24b901a --- /dev/null +++ b/C++/House-Robber-3.cpp @@ -0,0 +1,56 @@ +/** + * 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) {} + * }; + */ + +// Question link: https://leetcode.com/problems/house-robber-iii/ +class Solution +{ +public: + /* + Why is this DP on tree? And not simply greedy? + Try to think of some cases, like 5 -> 4 -> 2 -> 3 + here greedy takes only alternate levels sum -> max(5 + 2, 4 + 3) + and misses the final answer as 5 + 3 (choosing the main root and last leaf) giving 8 + + Case 1: you choose the current node + Case 2: you dont choose the current node + + Caching using map(unordered because its quicker than std::map & since we dont need nodes sorted in this question), to avoid TLE + */ + unordered_map mp; // maximum value obtained for this node, stored as pairs in map + int helper(TreeNode *root) + { + if (root == NULL) + return 0; + if (mp.find(root) != mp.end()) + return mp[root]; + int case1 = root->val; + if (root->left) + { + case1 += helper(root->left->left); + case1 += helper(root->left->right); + } + if (root->right) + { + case1 += helper(root->right->right); + case1 += helper(root->right->left); + } + int case2 = helper(root->left) + helper(root->right); + return mp[root] = max(case1, case2); + } + int rob(TreeNode *root) + { + if (root == NULL) + return 0; + int ans = helper(root); + return ans; + } +}; \ No newline at end of file From f96d028c9cc0faf59330e14d15fbec985ef92264 Mon Sep 17 00:00:00 2001 From: Simran-Sahni Date: Fri, 8 Oct 2021 12:56:13 +0530 Subject: [PATCH 2/4] specified complexity --- C++/House-Robber-3.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/C++/House-Robber-3.cpp b/C++/House-Robber-3.cpp index c24b901a..e85c8e59 100644 --- a/C++/House-Robber-3.cpp +++ b/C++/House-Robber-3.cpp @@ -24,6 +24,9 @@ class Solution Case 2: you dont choose the current node Caching using map(unordered because its quicker than std::map & since we dont need nodes sorted in this question), to avoid TLE + + Time Complexity: O(N) (because traversing over all N nodes of tree) + Space Complexity: O(N) (because of map) */ unordered_map mp; // maximum value obtained for this node, stored as pairs in map int helper(TreeNode *root) From 9b881f174c864c4d827a05b04771a35d7376475d Mon Sep 17 00:00:00 2001 From: Simran-Sahni Date: Fri, 8 Oct 2021 13:07:23 +0530 Subject: [PATCH 3/4] updated README --- README.md | 227 +++++++++++++++++++++++++++--------------------------- 1 file changed, 114 insertions(+), 113 deletions(-) diff --git a/README.md b/README.md index d9619791..badbaea3 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,8 @@ Issues Badge --> -### Got stuck in a LeetCode question? +### Got stuck in a LeetCode question? + ### This repository will help you by providing approach of solving the problems from LeetCode platform. ### [Contributors](#contributors) helped us in providing these Awesome solutions. @@ -82,15 +83,15 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu # Bit Manipulation -| # | Title | Solution | Time | Space | Difficulty | Tag | Tutorial | -| ---- | ------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | ---------------------------------------- | +| # | Title | Solution | Time | Space | Difficulty | Tag | Tutorial | +| ---- | ------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | ---------------------------------------- | | 136 | [Single Number](https://leetcode.com/problems/single-number/) | [Java](./Java/single-number.java)
[Python](./Python/single-number.py)
[C++](./C++/Single-Number.cpp)
[JavaScript](./JavaScript/single-number.js) | _O(n)_ | _O(1)_ | Easy | | Using XOR | -| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Python](./Python/single-number-ii.py)
[C++](./C++/Single-Number-II.cpp) | _O(n)_ | _O(1)_ | Medium | | | -| 260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Python](./Python/single-number-iii.py)
[C++](./C++/Single-Number-III.cpp) | _O(n)_ | _O(1)_ | Medium | | | -| 371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [Java](./Java/Sum_of_two_integers.java) | _O(1)_ | _O(1)_ | Medium | -| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Java](./Java/number-complement.java)
[C++](./C++/Number-Complement.cpp) | _O(1)_ | _O(1)_ | Easy | | [Tutorial](https://youtu.be/6bp5V-O3zts) | -| 520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Python](./Python/detect-capital.py)
[C++](./C++/Detect-Capital.cpp) | _O(n)_ | _O(1)_ | Easy | | | -| 1486 | [XOR Operation in an Array](https://leetcode.com/problems/xor-operation-in-an-array/) | [Java](./Java/xor-op-in-array.java)
[C++](./C++/xor-operation-in-an-array.cpp) | _O(n)_ | _O(1)_ | Easy | | Using XOR | +| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Python](./Python/single-number-ii.py)
[C++](./C++/Single-Number-II.cpp) | _O(n)_ | _O(1)_ | Medium | | | +| 260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Python](./Python/single-number-iii.py)
[C++](./C++/Single-Number-III.cpp) | _O(n)_ | _O(1)_ | Medium | | | +| 371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [Java](./Java/Sum_of_two_integers.java) | _O(1)_ | _O(1)_ | Medium | +| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Java](./Java/number-complement.java)
[C++](./C++/Number-Complement.cpp) | _O(1)_ | _O(1)_ | Easy | | [Tutorial](https://youtu.be/6bp5V-O3zts) | +| 520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Python](./Python/detect-capital.py)
[C++](./C++/Detect-Capital.cpp) | _O(n)_ | _O(1)_ | Easy | | | +| 1486 | [XOR Operation in an Array](https://leetcode.com/problems/xor-operation-in-an-array/) | [Java](./Java/xor-op-in-array.java)
[C++](./C++/xor-operation-in-an-array.cpp) | _O(n)_ | _O(1)_ | Easy | | Using XOR |
@@ -114,7 +115,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | # | Title | Solution | Time | Space | Difficulty | Note | Video Explaination | | ------- | ----------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- | ------------ | ------------- | ---------- | ------------------ | ---------------------------------------- | -| 118 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) | [Java](./Java/PascalsTriangle118.java) | _O(N^2)_ | _O(N)_ | Easy | | | +| 118 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) | [Java](./Java/PascalsTriangle118.java) | _O(N^2)_ | _O(N)_ | Easy | | | | 56 | [Merge Intervals](https://leetcode.com/problems/merge-intervals) | [Python](./Python/56_MergeIntervals.py) | _O(nlogn)_ | _O(n)_ | Medium | Intervals | | | 268 | [Missing Number](https://leetcode.com/problems/missing-number) | [Java](./Java/missing-number.java) | _O(n)_ | _O(1)_ | Easy | Array | [Tutorial](https://youtu.be/VwvGEE_OGss) | | 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array) | [Java](./Java/Degree-of-an-Array.java) | _O(n)_ | _O(n)_ | Easy | Array | | @@ -145,9 +146,8 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 27 | [Remove-Element](https://leetcode.com/problems/remove-element/) | [C++](./C++/remove-element.cpp) | O(N) | O(1) | Easy | Array | | 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Java](./Java/valid-sudoku.java) | O(N^2) | O(N) | Medium | Array, 2D Matrix | | 1512 | [Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs/) | [Java](./Java/Number-of-Good-Pairs.java) | O(N^2) | O(1) | Easy | Array | -| 162 | [Find Peak element](https://leetcode.com/problems/find-peak-element/) | [javascript](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/JavaScript/findPeakElement.js) | o(Logn) | O(1) | Medium | Array | -| 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [C++](./C++/Spiral-matrix.cpp) | O(M\*N) | O(M\*N) | Medium | Array | - +| 162 | [Find Peak element](https://leetcode.com/problems/find-peak-element/) | [javascript](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/JavaScript/findPeakElement.js) | o(Logn) | O(1) | Medium | Array | +| 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [C++](./C++/Spiral-matrix.cpp) | O(M\*N) | O(M\*N) | Medium | Array |
@@ -158,20 +158,21 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu # String -| # | Title | Solution | Time | Space | Difficulty | Tag | Note | -| :--: | ----------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | --------------- | -| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Python](./Python/Longest_Substring_Without_Repeating_Characters.py) | _O(n)_ | _O(n)_ | Medium | `Hash Table`
`Sliding Window` | Open for improvisation, mentioned time and space complexities unconfirmed | -| 8 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [Java](./Java/string-to-integer-atoi.java) | _O(n)_ | _O(1)_ | Medium | | | -| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Java](./Java/palindrome-number.java) | _O(n)_ | _O(1)_ | Easy | | | -| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [Java](./Java/reverse-words-in-a-string.java) | _O(1)_ | _O(n)_ | Medium | | | -| 383 | [Ransom Note](https://leetcode.com/problems/ransom-note/) | [Java](./Java/ransom-note.java) | _O(1)_ | _O(n)_ | Easy | | Character Count | -| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [Java](./Java/first-unique-character-in-a-string.java) | _O(n)_ | _O(1)_ | Easy | | Character Count | -| 520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Java](./Java/detect-capital-use.java) | _O(n)_ | _O(1)_ | Easy | | | -| 767 | [Reorganize String](https://leetcode.com/problems/reorganize-string/) | [Python](./Python/reorganize-string.py) | _O(n)_ | _O(n)_ | Medium | | | -| 859 | [Buddy Strings](https://leetcode.com/problems/buddy-strings/) | [Java](./Java/buddy-strings.java) | _O(n)_ | _O(1)_ | Easy | | | -| 1221 | [Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/) | [Python](./Python/split-a-string-in-balanced-strings.py) | _O(n)_ | _O(1)_ | Easy | | | -| 1374 | [Generate a String With Characters That Have Odd Counts](https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts/) | [Java](./Java/generate-a-string-with-characters-that-have-odd-counts.java) | _O(n)_ | _O(1)_ | Easy | | | -| 1614 | [Maximum Nesting Depth of the Parentheses](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/) | [Java](./Java/max-nesting-depth-parentheses.java) | _O(n)_ | _O(1)_ | Easy | | | +| # | Title | Solution | Time | Space | Difficulty | Tag | Note | +| :--: | ----------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ------ | ------ | ---------- | --------------------------------- | ------------------------------------------------------------------------- | +| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Python](./Python/Longest_Substring_Without_Repeating_Characters.py) | _O(n)_ | _O(n)_ | Medium | `Hash Table`
`Sliding Window` | Open for improvisation, mentioned time and space complexities unconfirmed | +| 8 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [Java](./Java/string-to-integer-atoi.java) | _O(n)_ | _O(1)_ | Medium | | | +| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Java](./Java/palindrome-number.java) | _O(n)_ | _O(1)_ | Easy | | | +| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [Java](./Java/reverse-words-in-a-string.java) | _O(1)_ | _O(n)_ | Medium | | | +| 383 | [Ransom Note](https://leetcode.com/problems/ransom-note/) | [Java](./Java/ransom-note.java) | _O(1)_ | _O(n)_ | Easy | | Character Count | +| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [Java](./Java/first-unique-character-in-a-string.java) | _O(n)_ | _O(1)_ | Easy | | Character Count | +| 520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Java](./Java/detect-capital-use.java) | _O(n)_ | _O(1)_ | Easy | | | +| 767 | [Reorganize String](https://leetcode.com/problems/reorganize-string/) | [Python](./Python/reorganize-string.py) | _O(n)_ | _O(n)_ | Medium | | | +| 859 | [Buddy Strings](https://leetcode.com/problems/buddy-strings/) | [Java](./Java/buddy-strings.java) | _O(n)_ | _O(1)_ | Easy | | | +| 1221 | [Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/) | [Python](./Python/split-a-string-in-balanced-strings.py) | _O(n)_ | _O(1)_ | Easy | | | +| 1374 | [Generate a String With Characters That Have Odd Counts](https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts/) | [Java](./Java/generate-a-string-with-characters-that-have-odd-counts.java) | _O(n)_ | _O(1)_ | Easy | | | +| 1614 | [Maximum Nesting Depth of the Parentheses](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/) | [Java](./Java/max-nesting-depth-parentheses.java) | _O(n)_ | _O(1)_ | Easy | | | +
⬆️ Back to Top @@ -180,20 +181,19 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu # Linked List -| # | Title | Solution | Time | Space | Difficulty | Tag | Tutorial | -| --- | --------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- | ---------- | ------ | ---------- | ------------------ | ---------------------------------------- | -| 002 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [Java](./Java/Add-Two-Numbers.java) | _O(n)_ | _O(n)_ | Medium | Math | | -| 19 | [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | [Java](./Java/remove-nth-node-from-end-of-list.java) | _O(n)_ | _O(1)_ | Medium | Two pointers | | -| 23 | [Merge K sorted lists](https://leetcode.com/problems/merge-k-sorted-lists/) | [C++](./C++/merge-k-sorted-lists.cpp) | _O(nlogn)_ | _O(n)_ | Hard | sorting and append | | -| 109 | [Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [Java](./Java/convert-sorted-list-to-binary-search-tree.java) | _O(n)_ | _O(n)_ | Medium | LinkedList | | -| 141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | [Java](./Java/linked-list-cycle.java) | _O(n)_ | _O(1)_ | Easy | Slow-Fast Pointers | | -| 142 | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [Java](./Java/linked-list-cycle-ii.java)
[C++](./C++/Linked-List-Cycle-II.cpp) | _O(n)_ | _O(1)_ | Medium | Slow-Fast Pointers | | -| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | [C++](./C++/LRU-Cache.cpp)
[Python](./Python/LRUCache.py) | _O(1)_ | _O(k)_ | Medium | Hash Map | | -| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [Java](./Java/intersection-of-two-linked-lists.java) | _O(n)_ | _O(1)_ | Easy | Two Pointers | [Tutorial](https://youtu.be/uozGB0-gbvI) | -| 186 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Java](./Java/middle-of-the-linked-list.java) | _O(n)_ | _O(1)_ | Easy | Two pointers | -| 143 | [Reorder List](https://leetcode.com/problems/reorder-list/) | [C++](./C++/143.Reorder_List.cpp) | _O(n)_ | _O(n)_ | Medium | Iteration and Stack | -| 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [C++](./C++/Swap-nodes-in-pairs.cpp) | _O(n)_ | _O(1)_ | Medium | Two pointers | - +| # | Title | Solution | Time | Space | Difficulty | Tag | Tutorial | +| --- | --------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- | ---------- | ------ | ---------- | ------------------- | ---------------------------------------- | +| 002 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [Java](./Java/Add-Two-Numbers.java) | _O(n)_ | _O(n)_ | Medium | Math | | +| 19 | [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | [Java](./Java/remove-nth-node-from-end-of-list.java) | _O(n)_ | _O(1)_ | Medium | Two pointers | | +| 23 | [Merge K sorted lists](https://leetcode.com/problems/merge-k-sorted-lists/) | [C++](./C++/merge-k-sorted-lists.cpp) | _O(nlogn)_ | _O(n)_ | Hard | sorting and append | | +| 109 | [Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [Java](./Java/convert-sorted-list-to-binary-search-tree.java) | _O(n)_ | _O(n)_ | Medium | LinkedList | | +| 141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | [Java](./Java/linked-list-cycle.java) | _O(n)_ | _O(1)_ | Easy | Slow-Fast Pointers | | +| 142 | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [Java](./Java/linked-list-cycle-ii.java)
[C++](./C++/Linked-List-Cycle-II.cpp) | _O(n)_ | _O(1)_ | Medium | Slow-Fast Pointers | | +| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | [C++](./C++/LRU-Cache.cpp)
[Python](./Python/LRUCache.py) | _O(1)_ | _O(k)_ | Medium | Hash Map | | +| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [Java](./Java/intersection-of-two-linked-lists.java) | _O(n)_ | _O(1)_ | Easy | Two Pointers | [Tutorial](https://youtu.be/uozGB0-gbvI) | +| 186 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Java](./Java/middle-of-the-linked-list.java) | _O(n)_ | _O(1)_ | Easy | Two pointers | +| 143 | [Reorder List](https://leetcode.com/problems/reorder-list/) | [C++](./C++/143.Reorder_List.cpp) | _O(n)_ | _O(n)_ | Medium | Iteration and Stack | +| 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [C++](./C++/Swap-nodes-in-pairs.cpp) | _O(n)_ | _O(1)_ | Medium | Two pointers |
@@ -203,21 +203,21 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu # Stack -| # | Title | Solution | Time | Space | Difficulty | Tag | Note | -| ---- | ------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------- | ------ | ------ | ---------- | ---------------------- | ---- | -| 020 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Python](./Python/20_ValidParentheses.py) [C++](./C++/Vlalid_Parentheses.cpp)[Java](./Java/20.ValidParentheses.java) | _O(n)_ | _O(n)_ | Easy | Stack | | -| 084 | [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/) | [C++](./C++/Largest-Rectangle-in-Histogram.cpp) | _O(n)_ | _O(n)_ | Hard | Stack | -| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [Python](./Python/150.EvaluateReversePolishNotation.py)
[Java](./Java/evaluate_reverse_polish_notation.java) | _O(n)_ | _O(1)_ | Medium | Stack | | -| 1047 | [Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [C++](./C++/remove-all-adjacent-duplicates-in-string.cpp) | _O(n)_ | _O(n)_ | Easy | Stack | | -| 682 | [Baseball Game](https://leetcode.com/problems/baseball-game/) | [C++](./C++/Baseball-Game.cpp) | _O(n)_ | _O(n)_ | Easy | Stack | | -| 1381 | [Design a Stack With Increment Operation](https://leetcode.com/problems/design-a-stack-with-increment-operation/) | [Java](./Java/Design-a-Stack-With-Increment-Operation.java) | _O(n)_ | _O(n)_ | Medium | Stack | | -| 1598 | [Crawler Log Folder](https://leetcode.com/problems/crawler-log-folder/) | [C++](./C++/Crawler-Log-Folder.cpp) | _O(n)_ | _O(n)_ | Easy | Stack | -| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Python](./Python/binary-tree-inorder-traversal.py) | _O(n)_ | _O(n)_ | Medium | Recursion, Binary Tree | -| 735 | [Asteroid Collision](https://leetcode.com/problems/asteroid-collision/) | [C++](./C++/asteroid-collision.cpp) | _O(n)_ | _O(1)_ | Medium | Stack | | -| 394 | [Decode String](https://leetcode.com/problems/decode-string/) | [C++](./C++/decode-string.cpp) | _O(n)_ | _O(1)_ | Medium | Stack | | -| 921 | [Minimum Add to Make Parentheses Valid](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/) | [C++](./C++/minimum-add-to-make-parentheses-valid.cpp) | _O(n)_ | _O(1)_ | Medium | Stack | | -| 32 | [Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/) | [Python](.Python/longest-valid-parentheses.py) | _O(n)_ | _O(n)_ | Hard | Stack | | -| 1249 | [Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/) | [C++](./C++/minimum-remove-to-make-valid-parentheses.cpp) | _O(n)_ | _O(n)_ | Medium | Stack | | +| # | Title | Solution | Time | Space | Difficulty | Tag | Note | +| ---- | ------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- | ------ | ------ | ---------- | ---------------------- | ---- | +| 020 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Python](./Python/20_ValidParentheses.py) [C++](./C++/Vlalid_Parentheses.cpp)[Java](./Java/20.ValidParentheses.java) | _O(n)_ | _O(n)_ | Easy | Stack | | +| 084 | [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/) | [C++](./C++/Largest-Rectangle-in-Histogram.cpp) | _O(n)_ | _O(n)_ | Hard | Stack | +| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [Python](./Python/150.EvaluateReversePolishNotation.py)
[Java](./Java/evaluate_reverse_polish_notation.java) | _O(n)_ | _O(1)_ | Medium | Stack | | +| 1047 | [Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [C++](./C++/remove-all-adjacent-duplicates-in-string.cpp) | _O(n)_ | _O(n)_ | Easy | Stack | | +| 682 | [Baseball Game](https://leetcode.com/problems/baseball-game/) | [C++](./C++/Baseball-Game.cpp) | _O(n)_ | _O(n)_ | Easy | Stack | | +| 1381 | [Design a Stack With Increment Operation](https://leetcode.com/problems/design-a-stack-with-increment-operation/) | [Java](./Java/Design-a-Stack-With-Increment-Operation.java) | _O(n)_ | _O(n)_ | Medium | Stack | | +| 1598 | [Crawler Log Folder](https://leetcode.com/problems/crawler-log-folder/) | [C++](./C++/Crawler-Log-Folder.cpp) | _O(n)_ | _O(n)_ | Easy | Stack | +| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Python](./Python/binary-tree-inorder-traversal.py) | _O(n)_ | _O(n)_ | Medium | Recursion, Binary Tree | +| 735 | [Asteroid Collision](https://leetcode.com/problems/asteroid-collision/) | [C++](./C++/asteroid-collision.cpp) | _O(n)_ | _O(1)_ | Medium | Stack | | +| 394 | [Decode String](https://leetcode.com/problems/decode-string/) | [C++](./C++/decode-string.cpp) | _O(n)_ | _O(1)_ | Medium | Stack | | +| 921 | [Minimum Add to Make Parentheses Valid](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/) | [C++](./C++/minimum-add-to-make-parentheses-valid.cpp) | _O(n)_ | _O(1)_ | Medium | Stack | | +| 32 | [Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/) | [Python](.Python/longest-valid-parentheses.py) | _O(n)_ | _O(n)_ | Hard | Stack | | +| 1249 | [Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/) | [C++](./C++/minimum-remove-to-make-valid-parentheses.cpp) | _O(n)_ | _O(n)_ | Medium | Stack | |
@@ -258,7 +258,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 968 | [Binary Tree Cameras](https://leetcode.com/problems/binary-tree-cameras/) | [C++](./C++/Binary-Tree-Cameras.cpp) | _O(n)_ | _O(logn)_ | Hard | Binary Tree, Dynamic Programming | | 98 | [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) | [Javascript](./JavaScript/98.Validate-Binary-Search-Tree.js) | _O(log(n))_ | _O(log(n))_ | Medium | Binary Tree | | 684 | [Redundant Connection](https://leetcode.com/problems/redundant-connection/) | [Java](./Java/Redundant-Connection/redundant-connection.java) | _O(N)_ | _O(N)_ | Medium | Tree, Union Find | -| 102 | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) |[C++](./C++/Binary-Tree-Level-Order-Traversal.cpp)| _O(n)_ | _O(n)_ | Medium | Binary Tree, map | | +| 102 | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [C++](./C++/Binary-Tree-Level-Order-Traversal.cpp) | _O(n)_ | _O(n)_ | Medium | Binary Tree, map | |
@@ -304,23 +304,23 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu # Math -| # | Title | Solution | Time | Space | Difficulty | Tag | Note | -| --- | ------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ----------------- | ------ | ---------- | ------ | ------------- | -| 050 | [Pow(x, n)](https://leetcode.com/problems/powx-n/) | [Python](./Python/50.Powxn.py)
[JavaScript](./JavaScript/50.Powxn.js) | _O(n)_ | _O(1)_ | Medium | Math | | -| 204 | [Count Primes](https://leetcode.com/problems/count-primes) | [C++](./C++/Count-Primes.cpp) | _O(n(log(logn)))_ | _O(n)_ | Easy | Math | | -| 171 | [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/) | [C++](./C++/Excel-Sheet-Column-Number.cpp) | _O(n)_ | _O(1)_ | Easy | String | | -| 168 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title) | [C++](./C++/Excel-Sheet-Column-Title.cpp) | _O(n)_ | _O(n)_ | Easy | String | | -| 007 | [Reverse Integer](https://leetcode.com/problems/reverse-integer) | [Java](./Java/reverse-integer.java)
[C++](./C++/Reverse-Integer.cpp) | _O(n)_ | _O(n)_ | Easy | Math | | -| 202 | [Happy Number](https://leetcode.com/problems/happy-number) | [Java](./Java/Happy-Number.java) | _O(n^2)_ | _O(n)_ | Easy | Math | | -| 326 | [Power of Three](https://leetcode.com/problems/power-of-three) | [Java](./Java/Power-of-Three.java) | _O(logn)_ | _O(n)_ | Easy | Math | | -| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman) | [Java](./Java/integer-to-roman.java) | _O(n)_ | _O(1)_ | Medium | Math | | -| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer) | [Java](./Java/roman-to-integer.java)
[C++](./C++/Roman_to_Integer.cpp)| _O(n)_ | _O(1)_ | Easy | Math | | -| 14 | [Arithmetic Subarrays](https://leetcode.com/problems/arithmetic-subarrays/) | [Java](./Java/Arithmetic-Subarrays.java) | _O(m\*n)_ | _O(n)_ | Medium | Math | Pattern Count | -| 263 | [Ugly Number](https://leetcode.com/problems/ugly-number/) | [Java](./Java/Ugly-Number.java) | _O(n)_ | _O(n)_ | Easy | Math | | -| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Java](./Java/FizzBuzz.java) | _O(n)_ | _O(n)_ | Easy | Math | | -| 1518 | [Water Bottles](https://leetcode.com/problems/water-bottles/) | [Java](./Java/WaterBottles.java) | _O(n)_ | _O(n)_ | Easy | Math | | -| 1822 | [Sign Of Product](https://leetcode.com/problems/sign-of-the-product-of-an-array/) | [Java](./Java/SignOf.java) | _O(n)_ | _O(n)_ | Easy | Math | | -| 991 | [Broken Calculator](https://leetcode.com/problems/broken-calculator/) | [Java](./Java/BrokenCalculator.java) | _O(n)_ | _O(n)_ | Medium | Math | | +| # | Title | Solution | Time | Space | Difficulty | Tag | Note | +| ---- | ------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- | ----------------- | ------ | ---------- | ------ | ------------- | +| 050 | [Pow(x, n)](https://leetcode.com/problems/powx-n/) | [Python](./Python/50.Powxn.py)
[JavaScript](./JavaScript/50.Powxn.js) | _O(n)_ | _O(1)_ | Medium | Math | | +| 204 | [Count Primes](https://leetcode.com/problems/count-primes) | [C++](./C++/Count-Primes.cpp) | _O(n(log(logn)))_ | _O(n)_ | Easy | Math | | +| 171 | [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/) | [C++](./C++/Excel-Sheet-Column-Number.cpp) | _O(n)_ | _O(1)_ | Easy | String | | +| 168 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title) | [C++](./C++/Excel-Sheet-Column-Title.cpp) | _O(n)_ | _O(n)_ | Easy | String | | +| 007 | [Reverse Integer](https://leetcode.com/problems/reverse-integer) | [Java](./Java/reverse-integer.java)
[C++](./C++/Reverse-Integer.cpp) | _O(n)_ | _O(n)_ | Easy | Math | | +| 202 | [Happy Number](https://leetcode.com/problems/happy-number) | [Java](./Java/Happy-Number.java) | _O(n^2)_ | _O(n)_ | Easy | Math | | +| 326 | [Power of Three](https://leetcode.com/problems/power-of-three) | [Java](./Java/Power-of-Three.java) | _O(logn)_ | _O(n)_ | Easy | Math | | +| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman) | [Java](./Java/integer-to-roman.java) | _O(n)_ | _O(1)_ | Medium | Math | | +| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer) | [Java](./Java/roman-to-integer.java)
[C++](./C++/Roman_to_Integer.cpp) | _O(n)_ | _O(1)_ | Easy | Math | | +| 14 | [Arithmetic Subarrays](https://leetcode.com/problems/arithmetic-subarrays/) | [Java](./Java/Arithmetic-Subarrays.java) | _O(m\*n)_ | _O(n)_ | Medium | Math | Pattern Count | +| 263 | [Ugly Number](https://leetcode.com/problems/ugly-number/) | [Java](./Java/Ugly-Number.java) | _O(n)_ | _O(n)_ | Easy | Math | | +| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Java](./Java/FizzBuzz.java) | _O(n)_ | _O(n)_ | Easy | Math | | +| 1518 | [Water Bottles](https://leetcode.com/problems/water-bottles/) | [Java](./Java/WaterBottles.java) | _O(n)_ | _O(n)_ | Easy | Math | | +| 1822 | [Sign Of Product](https://leetcode.com/problems/sign-of-the-product-of-an-array/) | [Java](./Java/SignOf.java) | _O(n)_ | _O(n)_ | Easy | Math | | +| 991 | [Broken Calculator](https://leetcode.com/problems/broken-calculator/) | [Java](./Java/BrokenCalculator.java) | _O(n)_ | _O(n)_ | Medium | Math | |
@@ -330,17 +330,16 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu # Breadth-First Search -| # | Title | Solution | Time | Space | Difficulty | Tag | Note | -| ---- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ------------------------- | ----------------- | ---------- | --- | ---- | -| 1284 | [Minimum Number of Flips to Convert Binary Matrix to Zero Matrix](https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/) | [C++](./C++/Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix.cpp) | _O(m * n * 2 ^ (m \* n))_ | _O(2 ^ (m \* n))_ | Hard | BFS | | -| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Java](./Java/NumberOfIslands.java) | O(R \* C) | O(R \* C) | Medium | BFS | -| 127 | [Word Ladder](https://leetcode.com/problems/word-ladder/) | [Java](./Java/word-ladder.java) | O(N^2 \* M) | O(N \* M) | Medium | BFS | -| 994 | [Rotten Oranges](https://leetcode.com/problems/rotting-oranges/) | [Python](./Python/994_Rotting_Oranges.py) | O(N \* M) | O(N \* M) | Medium | BFS | -| 743 | [Network Delay Time](https://leetcode.com/problems/network-delay-time/) | [C++](./C++/Network-delay-time.cpp) | _O(V+E))_ | O(V) | Medium | BFS | -| 111 | [Min Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [JavaScript](./JavaScript/111-minimum-depth-of-binary-tree.js) | O(nlogn) | O(nlogn) | Easy | BFS | -| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [C++](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/C%2B%2B/100_Same_Tree.cpp) | O(N) | O(N) | Easy | BFS | - - +| # | Title | Solution | Time | Space | Difficulty | Tag | Note | +| ---- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- | ------------------------- | ----------------- | ---------- | --- | ---- | +| 1284 | [Minimum Number of Flips to Convert Binary Matrix to Zero Matrix](https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/) | [C++](./C++/Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix.cpp) | _O(m * n * 2 ^ (m \* n))_ | _O(2 ^ (m \* n))_ | Hard | BFS | | +| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Java](./Java/NumberOfIslands.java) | O(R \* C) | O(R \* C) | Medium | BFS | +| 127 | [Word Ladder](https://leetcode.com/problems/word-ladder/) | [Java](./Java/word-ladder.java) | O(N^2 \* M) | O(N \* M) | Medium | BFS | +| 994 | [Rotten Oranges](https://leetcode.com/problems/rotting-oranges/) | [Python](./Python/994_Rotting_Oranges.py) | O(N \* M) | O(N \* M) | Medium | BFS | +| 743 | [Network Delay Time](https://leetcode.com/problems/network-delay-time/) | [C++](./C++/Network-delay-time.cpp) | _O(V+E))_ | O(V) | Medium | BFS | +| 111 | [Min Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [JavaScript](./JavaScript/111-minimum-depth-of-binary-tree.js) | O(nlogn) | O(nlogn) | Easy | BFS | +| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [C++](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/C%2B%2B/100_Same_Tree.cpp) | O(N) | O(N) | Easy | BFS | +
⬆️ Back to Top @@ -356,7 +355,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 112 | [Path Sum](https://leetcode.com/problems/path-sum/) | [Java](./Java/path-sum.java) | _O(n)_ | _O(n)_ | Easy | DFS | | | 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [Java](./Java/Balanced-Binary-Tree.java) | _O(n)_ | _O(n)_ | Easy | DFS | | | 1376 | [ Time Needed to Inform All Employees](https://leetcode.com/problems/time-needed-to-inform-all-employees/) | [C++](./C++/Cherry-Pickup-II.cpp) | _O(n)_ | _O(n)_ | Medium | DFS | | -| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [C++](./C++/number-of-islands.cpp) | _O(m * n)_ | _O(m * n)_ | Medium | DFS | | +| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [C++](./C++/number-of-islands.cpp) | _O(m \* n)_ | _O(m \* n)_ | Medium | DFS | |
@@ -366,12 +365,12 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu # BackTracking -| # | Title | Solution | Time | Space | Difficulty | Tag | Note | -| --- | ------------------------------------------------------------------- | --------------------------------- | ------------------------- | ----------- | ---------- | ------------------- | ---- | -| 037 | [Sudoku Solver](https://leetcode.com/problems/sudoku-solver/) | [C++](./C++/Sudoku-Solver.cpp) | _O(n^2)_ | _O(1)_ | Hard | Hash Table | | -| 980 | [Unique Paths III](https://leetcode.com/problems/unique-paths-iii/) | [C++](./C++/Unique-Paths-III.cpp) | _O(R * C * 2 ^ (R \* C))_ | _O(R \* C)_ | Hard | DFS, Memoization | | -| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [C++](./C++/combination-sum.cpp) | _O(2^n)_ | _O(n)_ | Medium | Array, Backtracking | | -| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [C++](./C++/letter-combinations-of-a-phone-number.cpp) | _O(4^n)_ | _O(n)_ | Medium | String, Hash Table, Backtracking | | +| # | Title | Solution | Time | Space | Difficulty | Tag | Note | +| --- | ------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------ | ------------------------- | ----------- | ---------- | -------------------------------- | ---- | +| 037 | [Sudoku Solver](https://leetcode.com/problems/sudoku-solver/) | [C++](./C++/Sudoku-Solver.cpp) | _O(n^2)_ | _O(1)_ | Hard | Hash Table | | +| 980 | [Unique Paths III](https://leetcode.com/problems/unique-paths-iii/) | [C++](./C++/Unique-Paths-III.cpp) | _O(R * C * 2 ^ (R \* C))_ | _O(R \* C)_ | Hard | DFS, Memoization | | +| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [C++](./C++/combination-sum.cpp) | _O(2^n)_ | _O(n)_ | Medium | Array, Backtracking | | +| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [C++](./C++/letter-combinations-of-a-phone-number.cpp) | _O(4^n)_ | _O(n)_ | Medium | String, Hash Table, Backtracking | |
@@ -381,20 +380,21 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu # Dynamic Programming -| # | Title | Solution | Time | Space | Difficulty | Tag | Note | -| ---- | ------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------- | --------- | --------- | ---------- | -------------------- | ---- | -| 416 | [ Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) | [C++](./C++/Partition-Equal-Subset-Sum.cpp) | _O(n^2)_ | _O(n^2)_ | Medium | DP | | -| 056 | [Wildcard Matching](https://leetcode.com/problems/wildcard-matching/) | [Python](./Python/wildcard-matching.py) | _O(n^2)_ | _O(n^2)_ | Hard | | -| 343 | [Integer Break](https://leetcode.com/problems/integer-break/) | [C++](./C++/Integer-Break.cpp) | _O(n^2)_ | _O(n)_ | Medium | | -| 139 | [Word Break](https://leetcode.com/problems/word-break/) | [Python](./Python/word-break-1.py) | _O(n^3)_ | _O(n)_ | Medium | DP | | -| 1092 | [Shortest Common Supersequence](https://leetcode.com/problems/shortest-common-supersequence/) | [C++](./C++/Shortest-Common-Supersequence.cpp) | _O(n^2)_ | _O(n^2)_ | Hard | DP | | -| 72 | [Edit Distance](https://leetcode.com/problems/edit-distance/) | [Python](./Python/edit-distance.py) | _O(N\*M)_ | _O(n^2)_ | Medium | Levenshtein Distance | | -| 91 | [Decode ways](https://leetcode.com/problems/decode-ways/) | [Python](./Python/decode-ways.py) | _O(N)_ | _O(N)_ | Easy | DP | | -| 1025 | [Divisor Game](https://leetcode.com/problems/divisor-game/) | [Python](./Python/divisor-game.py) | _O(N^2)_ | _O(N)_ | Easy | DP | | -| 174 | [Dungeon Game](https://leetcode.com/problems/dungeon-game/) | [C++](./C++/dungeon-game.pp) | _O(M\*N)_ | _O(M\*N)_ | Hard | Dynamic Programming | | -| 070 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Java](./Java/climbing-stairs.java) | _O(N)_ | _O(1)_ | Easy | DP | | -| 730 | [Count Different Palindromic Subsequences](https://leetcode.com/problems/count-different-palindromic-subsequences/) | [C++](./C++/Count-Different-Palindromic-Subsequences.cpp) | _O(N\*N)_ | _O(N\*N)_ | Hard | DP | | -| 55 | [Jump Game](https://leetcode.com/problems/jump-game/) | [Python](./Python/jumpGame.py) | _O(N)_ | _O(N)_ | Medium | DP | | +| # | Title | Solution | Time | Space | Difficulty | Tag | Note | +| ---- | ------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------- | --------- | --------- | ---------- | ----------------------- | ---- | +| 416 | [ Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) | [C++](./C++/Partition-Equal-Subset-Sum.cpp) | _O(n^2)_ | _O(n^2)_ | Medium | DP | | +| 056 | [Wildcard Matching](https://leetcode.com/problems/wildcard-matching/) | [Python](./Python/wildcard-matching.py) | _O(n^2)_ | _O(n^2)_ | Hard | | +| 343 | [Integer Break](https://leetcode.com/problems/integer-break/) | [C++](./C++/Integer-Break.cpp) | _O(n^2)_ | _O(n)_ | Medium | | +| 139 | [Word Break](https://leetcode.com/problems/word-break/) | [Python](./Python/word-break-1.py) | _O(n^3)_ | _O(n)_ | Medium | DP | | +| 1092 | [Shortest Common Supersequence](https://leetcode.com/problems/shortest-common-supersequence/) | [C++](./C++/Shortest-Common-Supersequence.cpp) | _O(n^2)_ | _O(n^2)_ | Hard | DP | | +| 72 | [Edit Distance](https://leetcode.com/problems/edit-distance/) | [Python](./Python/edit-distance.py) | _O(N\*M)_ | _O(n^2)_ | Medium | Levenshtein Distance | | +| 91 | [Decode ways](https://leetcode.com/problems/decode-ways/) | [Python](./Python/decode-ways.py) | _O(N)_ | _O(N)_ | Easy | DP | | +| 1025 | [Divisor Game](https://leetcode.com/problems/divisor-game/) | [Python](./Python/divisor-game.py) | _O(N^2)_ | _O(N)_ | Easy | DP | | +| 174 | [Dungeon Game](https://leetcode.com/problems/dungeon-game/) | [C++](./C++/dungeon-game.pp) | _O(M\*N)_ | _O(M\*N)_ | Hard | Dynamic Programming | | +| 070 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Java](./Java/climbing-stairs.java) | _O(N)_ | _O(1)_ | Easy | DP | | +| 730 | [Count Different Palindromic Subsequences](https://leetcode.com/problems/count-different-palindromic-subsequences/) | [C++](./C++/Count-Different-Palindromic-Subsequences.cpp) | _O(N\*N)_ | _O(N\*N)_ | Hard | DP | | +| 55 | [Jump Game](https://leetcode.com/problems/jump-game/) | [Python](./Python/jumpGame.py) | _O(N)_ | _O(N)_ | Medium | DP | | +| 343 | [House Robber 3](https://leetcode.com/problems/house-robber-iii/) | [C++](./C++/House-Robber-3.cpp) | _O(n)_ | _O(n)_ | Medium | DP Tree DFS Binary Tree |
@@ -421,7 +421,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu # Graph | # | Title | Solution | Time | Space | Difficulty | Tag | Note | -| ---- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------ | --------------- | --------- | ---------- | ----- | --------------------------------- | +| ---- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------ | --------------- | --------- | ---------- | ----- | --------------------------------- | --- | ------------------------------------------------------------ | ------ | ------- | ------ | ----- | ---------- | | 207 | [Course Schedule](https://leetcode.com/problems/course-schedule/) | [C++](./C++/Course-Schedule.cpp) | _O(V+E)_ | _O(V+E)_ | Medium | Graph | Cycle Detection in Directed Graph | | 1042 | [Flower Planting with No Adjacent](https://leetcode.com/problems/flower-planting-with-no-adjacent/) | [Python](./Python/1042_FlowerPlantingwithNoAdjacent.py) | _O(V+E)_ | _O(2V+E)_ | Medium | Graph | Graph Coloring | | 797 | [All Paths From Source to Target](https://leetcode.com/problems/all-paths-from-source-to-target/) | [Java](./Java/All_Paths_From_Source_to_Target.java) | _O(N^2)_ | _O(N)_ | Medium | Graph | DFS | @@ -429,7 +429,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 1192 | [Critical Connections in a Network](https://leetcode.com/problems/critical-connections-in-a-network/) | [C++](./C++/Critical-Connections-in-a-Network.cpp) | _O(V+E)_ | _O(4V+E)_ | Hard | Graph | Tarjan's Algorithm | | 113 | [Path Sum II](https://leetcode.com/problems/path-sum-ii/) | [C++](./C++/Critical-Path-Sum-II.cpp) | _O(V+E)_ | _O(V)_ | Medium | Graph | DFS | | 785 | [Is Graph Bipartite?](https://leetcode.com/problems/is-graph-bipartite/) | [C++](./C++/Is-Graph-Bipartite.cpp) | _O(V+E)_ | _O(V)_ | Medium | Graph | BFS | -| 947 | [Most Stones Removed with Same Row or Column](https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/) | [C++](./C++/Most-Stones-Removed-with-Same-Row-or-Column.cpp) | _O(V)_ | _O(2V)_ | Medium | Graph | Union Find | | [C++](./C++/Most-Stones-Removed-with-Same-Row-or-Column.cpp) | _O(V)_ | _O(2V)_ | Medium | Graph | Union Find | +| 947 | [Most Stones Removed with Same Row or Column](https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/) | [C++](./C++/Most-Stones-Removed-with-Same-Row-or-Column.cpp) | _O(V)_ | _O(2V)_ | Medium | Graph | Union Find | | [C++](./C++/Most-Stones-Removed-with-Same-Row-or-Column.cpp) | _O(V)_ | _O(2V)_ | Medium | Graph | Union Find | | 210 | [Course Schedule II](https://leetcode.com/problems/course-schedule-ii/) | [C++](./C++/Course-Schedule-II.cpp) | _O(V+E)_ | _O(V)_ | Medium | Graph | BFS | | 1627 | [Graph Connectivity with Threshold](https://leetcode.com/problems/graph-connectivity-with-threshold/) | [Java](./Java/graph_connectivity_with_threshold.java) | _O(V.logV + Q)_ | _O(V)_ | Hard | Graph | Union Find + Sieve | | 1631 | [Path with Minimum Effort](https://leetcode.com/problems/path-with-minimum-effort/) | [Java](./Java/path-with-minimum-effort.java) | _O(V^2)_ | _O(V)_ | Medium | Graph | Dijkstra's Shortest Path | @@ -508,11 +508,12 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if | [Aysia](https://www.linkedin.com/in/aysiaelise/)
| USA | JavaScript | [GitHub](https://github.com/aysiae) | | [Poorvi Garg](https://github.com/POORVI111)
| India | C++ | [GitHub](https://github.com/POORVI111) | | [Lakshmanan Meiyappan](https://laxmena.com)
| India | C++ | [Website - Blog](https://laxmena.com)
[GitHub](https://github.com/laxmena)
[LinekdIn](https://www.linkedin.com/in/lakshmanan-meiyappan/) | -| [Sachin_Upadhyay](https://github.com/sachsbu)
| India | Java | [GitHub](https://github.com/sachsbu) -| [Amisha Sahu](https://github.com/Amisha328)
| India | C++ | [CodeChef](https://www.codechef.com/users/amisha328)
[LeetCode](https://leetcode.com/Mishi328/)
[HackerRank](https://www.hackerrank.com/amishasahu328) -| [Shrimadh V Rao](https://github.com/Shrimadh)
| India | C++ | [GitHub](https://github.com/Shrimadh) -| [Shreyas Shrawage](https://github.com/shreyventure)
| India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)
[LeetCode](https://leetcode.com/shreyventure/)
[HackerRank](https://www.hackerrank.com/shreyas_shrawage) -| [Surbhi Mayank](https://github.com/surbhi2408)
| India | C++ | [GitHub](https://github.com/surbhi2408) +| [Sachin_Upadhyay](https://github.com/sachsbu)
| India | Java | [GitHub](https://github.com/sachsbu) | +| [Amisha Sahu](https://github.com/Amisha328)
| India | C++ | [CodeChef](https://www.codechef.com/users/amisha328)
[LeetCode](https://leetcode.com/Mishi328/)
[HackerRank](https://www.hackerrank.com/amishasahu328) | +| [Shrimadh V Rao](https://github.com/Shrimadh)
| India | C++ | [GitHub](https://github.com/Shrimadh) | +| [Shreyas Shrawage](https://github.com/shreyventure)
| India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)
[LeetCode](https://leetcode.com/shreyventure/)
[HackerRank](https://www.hackerrank.com/shreyas_shrawage) | +| [Surbhi Mayank](https://github.com/surbhi2408)
| India | C++ | [GitHub](https://github.com/surbhi2408) | + From 98549c2c90cbcb77d1fdd72e31a0903569d929b6 Mon Sep 17 00:00:00 2001 From: Simran-Sahni Date: Fri, 8 Oct 2021 14:50:01 +0530 Subject: [PATCH 4/4] Tree-DP question added --- C++/Sum-of-distances-in-tree.cpp | 66 ++++++++++++++++++++++++++++++++ README.md | 1 + 2 files changed, 67 insertions(+) create mode 100644 C++/Sum-of-distances-in-tree.cpp diff --git a/C++/Sum-of-distances-in-tree.cpp b/C++/Sum-of-distances-in-tree.cpp new file mode 100644 index 00000000..217a226f --- /dev/null +++ b/C++/Sum-of-distances-in-tree.cpp @@ -0,0 +1,66 @@ +class Solution +{ +public: + /* + Uses classics Tree Re-rooting DP, + Need to do 2 traversals: DFSes but 1 as Post Order and second as Pre-order + + When we move our root from parent to its child i, + count[i] points get 1 closer to root, n - count[i] nodes get 1 futhur to root. + + Time Complexity: O(N) because both DFS visit all ndoes + Space Compexity: O(N) because of the resultant & helper subtree-node-count array + + */ + + /* Post Order to update distances while going from parent to root + To track a count of number of children in the subtree + */ + void dfs1(int node, int parent, vector &res, vector &count, vector> &graph) + { + for (auto child : graph[node]) + { + if (child == parent) + continue; + dfs1(child, node, res, count, graph); + count[node] += count[child]; + res[node] += res[child] + count[child]; + } + } + + /* Pre order traversal style DFS, to update the result vector in direction of child -> parent */ + void dfs2(int node, int parent, vector &res, vector &count, vector> &graph, int n) + { + for (auto child : graph[node]) + { + if (child == parent) + continue; + res[child] = res[node] - count[child] + n - count[child]; + dfs2(child, node, res, count, graph, n); + } + } + + vector sumOfDistancesInTree(int n, vector> &edges) + { + + vector count(n, 1); //stores count of number of children, atleast 1 node, ie itself should be in count[node] + vector res(n, 0); //Stores final distance sum for each node + + vector> graph(n); + + int u, v; + + // Constructing the graph from the given array + for (int i = 0; i < edges.size(); ++i) + { + u = edges[i][0], v = edges[i][1]; + graph[u].push_back(v); + graph[v].push_back(u); + } + + dfs1(0, -1, res, count, graph); + dfs2(0, -1, res, count, graph, n); + + return res; + } +}; \ No newline at end of file diff --git a/README.md b/README.md index badbaea3..8cfcf83b 100644 --- a/README.md +++ b/README.md @@ -395,6 +395,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 730 | [Count Different Palindromic Subsequences](https://leetcode.com/problems/count-different-palindromic-subsequences/) | [C++](./C++/Count-Different-Palindromic-Subsequences.cpp) | _O(N\*N)_ | _O(N\*N)_ | Hard | DP | | | 55 | [Jump Game](https://leetcode.com/problems/jump-game/) | [Python](./Python/jumpGame.py) | _O(N)_ | _O(N)_ | Medium | DP | | | 343 | [House Robber 3](https://leetcode.com/problems/house-robber-iii/) | [C++](./C++/House-Robber-3.cpp) | _O(n)_ | _O(n)_ | Medium | DP Tree DFS Binary Tree | +| 343 | [Sum of Distances in Tree](https://leetcode.com/problems/sum-of-distances-in-tree/) | [C++](./C++/Sum-of-distances-in-tree.cpp) | _O(n)_ | _O(n)_ | Hard | DP Tree DFS Graph | [Youtube Tutorial](https://www.youtube.com/watch?v=JfBzxIBrnKA)