Solutions in C++ & Python 3 for LeetCode problems. New problems will be continuously added.
To compile a C++ solution run:
gcc -Wall path/to/the.cpp -o main -lc++ -std=c++11
./main
Note: For LinkedLists and Trees add parse_input.cpp like this:
gcc -Wall path/to/the.cpp path/to/parse_input.cpp -o main -lc++ -std=c++11
./main
For Python 3 just do:
python3 path/to/the.py
And follow the prompts.
- Arrays
| Problem | Solution | Time complexity | Space complexity |
|---|---|---|---|
| 48. Rotate Image | cpp py | O(N2) | O(1) |
| 53. Maximum Subarray | cpp py | O(N) | O(1) |
| 55. Jump Game | cpp py | O(N) | O(1) |
| 283. Move Zeroes | cpp py | O(N) | O(1) |
- Linked Lists
| Problem | Solution | Time complexity | Space complexity |
|---|---|---|---|
| 2. Add Two Numbers | cpp py | O(M + N) | O(max(M, N)) |
| 19. Remove Nth Node From End of List | cpp py | O(N) | O(1) |
| 21. Merge Two Sorted Lists | cpp py | O(N + M) | O(1) |
| 206. Reverse Linked List | cpp py | O(N) | O(1) |
- Math
| Problem | Solution | Time complexity | Space complexity |
|---|---|---|---|
| 509. Fibonacci Number | cpp py | O(N) | O(1) |
| 1414. Find the Minimum Number of Fibonacci Numbers Whose Sum Is K | cpp py | O(logk) | O(1) |
- Sorting
| Problem | Solution | Time complexity | Space complexity |
|---|---|---|---|
| 75. Sort Colors | cpp py | O(N), one pass | O(1) |
| 179. Largest Number | cpp py | O(SNlogN)* | O(SN)* |
*S = logEL - average number of digits in an element of the array
- Strings
| Problem | Solution | Time complexity | Space complexity |
|---|---|---|---|
| 20. Valid Parentheses | cpp py | O(N) | O(N) |
| 299. Bulls and Cows | cpp py | O(N) | O(1) |
| 438. Find All Anagrams in a String | cpp py | O(P + S) | O(1) |
| 771. Jewels and Stones | cpp py | O(J + S) | O(J) |
| 921. Minimum Add to Make Parentheses Valid | cpp py | O(N) | O(1) |
- Trees
| Problem | Solution | Time complexity | Space complexity |
|---|---|---|---|
| 94. Binary Tree Inorder Traversal | cpp py | O(N) | O(logN) |
| 102. Binary Tree Level Order Traversal | cpp py | O(N) | O(N) |
| 104. Maximum Depth of Binary Tree | cpp py | O(N) | DFS: O(logN) | BFS: O(N) |
| 111. Minimum Depth of Binary Tree | cpp py | O(N) | DFS: O(logN) | BFS: O(N) |
| 199. Binary Tree Right Side View | cpp py | O(N) | O(logN) |