|
1 | 1 | # Leetcode
|
2 | 2 |
|
3 |
| -Hints + solutions for ~250 leetcode problems. Mostly medium, some hard. Currently adding more hints as I revisit my solutions. Please help me add more hints if you can. |
| 3 | +Hints for ~250 leetcode problems. Mostly medium, some hard. |
| 4 | +Good practice list for internships/job hunt. |
4 | 5 |
|
5 |
| -I commit a lot, many a times unnecessarily in this repo, it could be better solution or some optimization, or alternate solution, or variable renaming. I apologize for that as well. |
| 6 | +Solutions <a href = "https://github.com/aliasvishnu/leetcode/tree/master/Solutions" > here </a> |
6 | 7 |
|
7 |
| -## First hint list |
| 8 | +## Abbreviations |
| 9 | +* BF - Brute Force, |
| 10 | +* DP - Dynamic Programming |
| 11 | +* Array - Problem specific array logic |
| 12 | +* D&C - Divide & Conquer |
8 | 13 |
|
| 14 | +## First hint list |
9 | 15 |
|
10 |
| -| # | Hints | Complexity | |
11 |
| -|---| ----- | ---------- | |
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))| |
29 |
| -| 36 | Brute force.| O(1)| |
30 |
| -| 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 | |
35 |
| -| 108 | Where does the root element fall during an inorder (sorted) traversal? | | |
36 |
| -| 128 | DP problem, find recurrence relation. | | |
| 16 | +| # | Tags | Hints | Complexity | |
| 17 | +|---| ---- | ----- | ---------- | |
| 18 | +| 1 | Hash | Use hash map to store all values until current value. | O(n) | |
| 19 | +| 2 | BF | Brute force. | O(n) | |
| 20 | +| 3 | Array | Use a window. Extend if next element is not in the current window. | O(n) | |
| 21 | +| 5 | DP | Build recurrence relation (may help to solve longest common substring problem) | O(n^2) | |
| 22 | +| 7 | BF | Brute force. | O(log(n)) | |
| 23 | +| 9 | BF | Brute force. | O(log(n)) | |
| 24 | +| 10 | DP | Use a 2D matrix. DP[i][j] = whether p[0:i] matches with str[0:j]. | O(mn) | |
| 25 | +| 15 | Array | Solve 2 sum before attempting this problem. | O(n^2) | |
| 26 | +| 16 | Array | Similar to 3 sum, but use a sorted array and make front++, back-- updates to minimize abs(target - sum). | O(n^2) | |
| 27 | +| 17 | Recursion | Possible approaches are trie, or more easy - building on smaller solution. | O(4^n)| |
| 28 | +| 22 | Recursion | If less than n open brackets are open, we can add open bracket. But to add a close bracket you need count(')') < count('(') | . | |
| 29 | +| 23 | Heap | Use a data structure which can give you minimum element at low cost.| . | |
| 30 | +| 26 | Array | Maintain length of unique elements till current element.| O(n)| |
| 31 | +| 27 | Array | vector.erase() will not work, as vector overwrites the element every time erase is called.| O(n)| |
| 32 | +| 28 | BF | Brute force. | O(mn) | |
| 33 | +| 31 | Array, Sorting | Find the location closest from the end which has segment ab of the form a < b, and make it ba. | O(n log(n))| |
| 34 | +| 33 | Binary Search | Array increasing, sudden drop and then increasing again. Find out where the middle element is, can it's location help you? | O(log(n))| |
| 35 | +| 36 | BF | Brute force.| O(1)| |
| 36 | +| 42 | Stack + Array | 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. | O(n) | |
| 37 | +| 43 | BF | Normal multiplication, like in school. | O(n log(d)) | |
| 38 | +| 44 | DP | 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) | |
| 39 | +| 46 | Recursion | Combine str[0] with permutations of str[1:], use recursion. | O(n!) | |
| 40 | +| 47 | Recursion | Eg, aaaabbcc, only one of each kind {a, b, c} can come at 0th index. Recurse. | O(n!) | |
| 41 | +| 48 | Array | index <i, j> is i positions from left, j positions from top. Find a mapping to 3 other locations, and swap the values. | O(n^2) | |
| 42 | +| 49 | Hash | Hashing | O(n) if O(1) hash | |
| 43 | +| 50 | D&C/Recursion | 2^n = 2^(n/2)*2^(n/2) | O(log(n)) | |
| 44 | +| 51 | Backtracking + Pruning | If a queen is in <i, j>, block that diagonal, row & column. | O(n!) | |
| 45 | +| 52 | Backtracking | Solution for 51 is more exhaustive than this problem. | O(n!) | |
| 46 | +| 53 | Array | Keep extending window <i, j> to the right until sum > 0, if sum < 0, set i = j+1 | O(n) | |
| 47 | +| 54 | Array/BF | Brute Force | O(n^2)| |
| 48 | +| 56 | Sorting | Sort based on start time. | O(n log(n)) | |
| 49 | +| 57 | Sorting | Insert interval and do problem 56. | O(n log(n)) | |
| 50 | +| 66 | BF | Brute force | O(n) | |
| 51 | +| 67 | BF | Simple array addition | O(log(n)) | |
| 52 | +| 68 | Array | If you can fit in k words in a line, there must by atleast k-1 spaces, so L-(k-1)-sum(char's in words) must be divided mentioned in the question. | O(n) | |
| 53 | +| 69 | Binary Search | start = 0, end = x/2, binary search | O(log(n)) | |
| 54 | +| 70 | DP | DP[i] = DP[i-1] + DP[i-2] | O(n)| |
| 55 | +| 71 | BF | Split by '/', use stack to handle '..' | O(n) | |
| 56 | +| 108 | Inorder traversal | Where does the root element fall during an inorder (sorted) traversal? | | |
| 57 | +| 128 | DP | DP problem, find recurrence relation. | | |
37 | 58 |
|
38 | 59 | ## Second hint list
|
39 | 60 |
|
|
0 commit comments