Skip to content

Commit 1f4a438

Browse files
committed
hints
1 parent 67f0c5d commit 1f4a438

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

README.md

+26-13
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,46 @@ I commit a lot, many a times unnecessarily in this repo, it could be better solu
99

1010
| # | Hints | Complexity |
1111
|---| ----- | ---------- |
12-
| 5 | Build recurrence relation (may help to solve longest common substring problem) |. |
13-
| 15 | Solve 2 sum before attempting this problem. |. |
14-
| 17 | Possible approaches are trie, or more easy - building on smaller solution. | |
15-
| 22 | If less than n open brackets are open, we can add open bracket. But to add a close bracket you need count(')') < count('(') | .|
16-
| 23 | Use a data structure which can give you minimum element at low cost.|. |
17-
| 26 | Maintain length of unique elements till current element.|.|
18-
| 27 | vector.erase() will not work, as vector overwrites the element every time erase is called.|. |
19-
| 28 | Brute force. | .|
20-
| 31 | Find the location closest from the end which has segment ab of the form a < b, and make it ba. | .|
21-
| 33 | Array increasing, sudden drop and then increasing again. Find out where the middle element is, can it's location help you? | .|
12+
| 1 | Use hash map to store all values until current value. | O(n) |
13+
| 2 | Brute force. | O(n) |
14+
| 3 | Use a window. Extend if next element is not in the current window. | O(n) |
15+
| 5 | Build recurrence relation (may help to solve longest common substring problem) | O(n^2) |
16+
| 7 | Brute force. | O(log(n)) |
17+
| 9 | Brute force. | O(log(n)) |
18+
| 10 | Use a 2D matrix. DP[i][j] = whether p[0:i] matches with str[0:j]. | O(mn) |
19+
| 15 | Solve 2 sum before attempting this problem. | O(n^2) |
20+
| 16 | Similar to 3 sum, but use a sorted array and make front++, back-- updates to minimize abs(target - sum). | O(n^2) |
21+
| 17 | Possible approaches are trie, or more easy - building on smaller solution. | O(4^n)|
22+
| 22 | If less than n open brackets are open, we can add open bracket. But to add a close bracket you need count(')') < count('(') | . |
23+
| 23 | Use a data structure which can give you minimum element at low cost.| . |
24+
| 26 | Maintain length of unique elements till current element.| O(n)|
25+
| 27 | vector.erase() will not work, as vector overwrites the element every time erase is called.| O(n)|
26+
| 28 | Brute force. | O(mn) |
27+
| 31 | Find the location closest from the end which has segment ab of the form a < b, and make it ba. | O(n log(n))|
28+
| 33 | Array increasing, sudden drop and then increasing again. Find out where the middle element is, can it's location help you? | O(log(n))|
2229
| 36 | Brute force.| O(1)|
2330
| 42 | Find next greater element index for each index, and prefix sum for each element. Can you build the solution from this? Check out problem 496 for next greater element. | |
31+
| 43 | Normal multiplication, like in school. | O(n log(d)) |
32+
| 44 | Solve problem 10 first. Construct a 2D box of size len(pattern)*len(string). DP[i][j] = whether p[0:i] matches with str[0:j] | O(mn) |
33+
| 46 | Combine str[0] with permutations of str[1:], use recursion. | O(n!) |
34+
| 47 |
2435
| 108 | Where does the root element fall during an inorder (sorted) traversal? | |
2536
| 128 | DP problem, find recurrence relation. | |
2637

2738
## Second hint list
2839

2940
| # | Hints | Complexity |
3041
|---| ----- | ---------- |
31-
| 5 | Use DP, P<i, j> = P<i+1, j-1> && S[i] == S[j] is the recurrence relation.| |
32-
| 15 | 2sum for a sorted list gives a more optimal sub routine here. | |
33-
| 17 | Build solution for first k numbers, and modify solution vector for k+1 number. | |
42+
| 5 | Use DP, P<i, j> = P<i+1, j-1> && S[i] == S[j] is the recurrence relation.| O(n^2) |
43+
| 10 | Manually solve [aab, c*a*b], [ab, .b], [aaa, ab*a] for corner cases. | O(mn) |
44+
| 15 | 2sum for a sorted list gives a more optimal sub routine here. | O(n^2)|
45+
| 17 | Build solution for first k numbers, and modify solution vector for k+1 number. | O(4^n)|
3446
| 22 | Backtracking based solution. | |
3547
| 23 | Use heap (this method uses extra memory). | |
3648
| 27 | Count the number of occurences in a variable - acting as offset. | |
3749
| 31 | Sort from b onwards. Solution is done. | |
3850
| 33 | Depending upon the location of the middle element and whether target is lower or greater than middle element, write 4 cases. | |
3951
| 42 | Let nge(i) = k and p be prefix sum array, we can roughly say water trapped = Sigma[ (h_i x (k-i)) - (p[k]-p[i])] | |
52+
| 44 | Write cases for when p[i] = '*' and not '*'. | O(mn) |
4053
| 108 | Left node must be filled before right node.| |
4154
| 128 | If L[k] represents the largest consecutive sequence, L[k] -> L[k-L[k]]+ L[k] | |

0 commit comments

Comments
 (0)