LeetCode
LeetCode copied to clipboard
My LeetCode practice source code and notes.
David's LeetCode Practice

Testing
- local dependencies
pytestpytest-covcoverage
Correctness (based on pytest)
Test all the units (in the main directory):
py.test -v
Code Coverage
pytest --cov-report term --cov Python3/
if successful you should see a new
.coveragefile
coverage report
- Coverage.py — Coverage.py 4.3.4 documentation
- Code Coverage Done Right | Codecov
- codecov/codecov-python: Python report uploader for Codecov
- codecov/example-python: Python coverage example
- Frequently Asked Questions - Where is the repository upload token found?
- Excluding code from coverage.py — Coverage.py 4.3.4 documentation
Python3 Progress
Python 3.8
- Virtual Environment
- Install Latest Python
- Python Docker
docker pull python:3.8
Symbols
- Difficulty:
- Harder: ▲
- Same: -
- Easier: ▼
- Important: *
- Good: 👍
Abbreviation
- Algorithm
- DP - Dynamic Programming
- DC - Divide and Conquer
- Data Structure
- HT - Hash Table
- BST - Binary Search Tree
- PQ - Priority Queue (usually Heap)
- Others
- BM - Bit Manipulation
Naive means the first thought of mine (usually a little better than Brute-Force, but may need to be optimized.)
LeetCode Patterns
- Leetcode Patterns – Medium
- Leetcode Pattern 0 - Iterative traversals on Trees
- Leetcode Pattern 1 - DFS + BFS
- Leetcode Pattern 2 - Sliding Windows for Strings
- Leetcode Pattern 3 - Backtracking
- Leetcode Pattern 4 - Meta Stuff
Remark
- Category
Category1, Category2, ...- Usually record in (input data) Data Structure. (The Algorithm (and additional/special Data Structure) strategy will be noted in the Method field)
- Code will be put in the main category folder (i.e. the first one).
- Some category may be the Related Topic tags marked by LeetCode.
- (Add Pattern 0~4 in brackets)
- TODO: Most of the categories need to be updated. (The date before 2018/9/22)
- TODO: Mark "Good for Beginner" or "Good for Intermediate" etc.
- TODO: Tags for search (special mark in file or filename)
| Number | Difficulty | Problem | Date | Category | Method-TimeComplexity | Remark | TODO |
|---|---|---|---|---|---|---|---|
| 👍 001 | Easy | Two Sum | 2018/3/12 | Array, HT | Naive-O(nlogn), HT-O(n), Sorted HT-O(n) | Note | - |
| 002 | Medium | Add Two Numbers | 2018/3/12 | Linked List | Naive-O(n) | Note | Make a shorter version |
| 👍 003 | Medium * | Longest Substring Without Repeating Characters | 2018/9/24 | String, HT | Sliding Window, Sliding Window Optimized | Note | - |
| 👍 005 | Medium * | Longest Palindromic Substring | 2019/2/28 | String, DP | Naive-O(n³), DP-O(n²), Expand Around Center-O(n²) | Note | - |
| 006 | Medium | ZigZag Conversion | 2018/3/22 | Array | Naive-O(n) | Note | - |
| 007 | Easy | Reverse Integer | 2018/3/22 | Array | Naive-O(n) | Note | - |
| 008 | Medium | String to Integer (atoi) | 2018/9/30 | String (TODO: move to AdHoc) | Naive-O(n) | Note | - |
| 009 | Easy | Palindrome Number | 2018/3/22 | Array | Naive-O(n), Without using string-O(n) | Note | - |
| 010 | Hard * | Regular Expression Matching | 2018/10/13 | String | DC-O((m+n) x 2ⁿ), Top-Down DP-O(mn), Bottom-Up DP-O(mn) | Note | - |
| 👍 011 | Medium * | Container With Most Water | 2018/9/22 | Array | Naive-O(n²), Two Pointer-O(n), Naive 2-O(n²), Two Pointer 2-O(n) | Note | - |
| 012 | Medium | Integer to Roman | TODO | String (TODO: move to AdHoc) | Naive-O(n) | Note | - |
| 013 | Easy | Roman to Integer | 2018/9/29 | String (TODO: move to AdHoc) | Naive-O(n) | Note | - |
| 014 | Easy | Longest Common Prefix | 2018/9/16 | Array | Naive-O(n*len) | Note | - |
| 👍 015 | Medium ▲ | 3Sum | 2018/10/13 | Array | TwoPointer-O(n²), Naive-O(n²), TwoPointer2-O(n²), BinarySearch-O(n²) | Note | do it again |
| 016 | Medium | 3Sum Closest | 2018/10/13 | Array | TwoPointer-O(n²) | Note | - |
| 017 | Medium | Letter Combinations of a Phone Number | 2019/3/1 | String | Backtracking-O(n!) | Note | - |
| 👍 018 | Medium ▲ | 4Sum | 2019/3/2 | Array, HT | GeneralizedTwoPointer-O(n³) | Note | - |
| 019 | Medium * | Remove Nth Node From End of List | 2019/3/3 | Linked List | HT-O(n), TwoPointer-O(n) | Note, Sina Interview | do it again |
| 👍 020 | Easy | Valid Parentheses | 2018/9/15 | String, Stack | Naive-O(n), Stack-O(n), HashTable-O(n) | Note, Learn: Queue & Stack | - |
| 021 | Easy * | Merge Two Sorted Lists | 2018/6/27 | Linked List | Better-O(n), Naive-O(n) | Note | - |
| 👍 023 | Hard * | Merge k Sorted Lists | 2018/6/26 | Linked List | Naive-O(kn), Naive 2-O(nlogn), DC-O(nlogk), DC 2-O(nlogk), Heap-O(nlogk) | Note | Compare K each time, attrgetter |
| 👍 022 | Medium * | Generate Parentheses | 2019/3/6 | String | Backtracking-O(n!), Naive-O(n!) | Note | - |
| 024 | Medium | Swap Nodes in Pairs | 2018/5/31 | Linked List | Naive-O(n), Recursive-O(n) | Note | - |
| 026 | Easy | Remove Duplicates from Sorted Array | 2018/6/14 | Array | Naive-O(n), Better-O(n) | Note | - |
| 027 | Easy | Remove Element | 2019/7/2 | Array | Naive-O(n), Naive2-O(n) | Note | - |
| 028 | Easy | Implement strStr() | 2019/7/2 | String | Naive-O(n) | Note | - |
| 031 | Medium ▲ | Next Permutation | 2019/3/4 | Array | SinglePass-O(n) | Note | Permutation with duplicates |
| 👍 033 | Medium * | Search in Rotated Sorted Array | 2019/3/5 | Array | BinarySearch-O(log(n)), BinarySearch2-O(log(n)) | Note, FreeWheel 2020 Fall Recruit | do it again |
| 👍 035 | Easy | Search Insert Position | 2019/7/3 | Array | BinarySearch-O(log(n)), Naive-O(log(n)), BinarySearch2-O(log(n)) | Note | - |
| 036 | Medium ▼ | Valid Sudoku | 2018/6/21 | Array | HT-O(n) | Note | - |
| 037 | Hard | Sudoku Solver | TODO | Array | Note | - | |
| 038 | Easy | Count and Say | 2019/9/30 | String | Naive-O(n) | Note | - |
| 039 | Medium | Combination Sum | 2019/3/7 | Array | Backtracking-O(n!), Naive-O(n!) | Note | improve time complexity by not use sort |
| 👍 042 | Hard * | Trapping Rain Water | TODO | Array | - | ||
| 👍 044 | Hard | Wildcard Matching | TODO | String | Note | DP, NFA | |
| 👍 045 | Medium * | Jump Game II | 2020/10/14 | Array | DP-O(n^2), Greedy-O(n^2), BFS-O(n^2), Greedy 2-O(n^2) | do it again with all solutions | |
| 👍 046 | Medium * | Permutations | 2018/5/31 | Array | Backtracking-O(n!), Recursive-O(n!), DFS Based-O(n!) | Note | - |
| 👍 048 | Medium * | Rotate Image | 2019/12/2 | Array | Naive-O(n) | Note, Microsoft 2020 Fall Recruit | - |
| 👍 049 | Medium * | Group Anagrams | 2019/12/3 | String, HT | Naive-O(nk), Sorting-O(nklogk), Counter-O(nk) | Note | testcase |
| 👍 050 | Medium * | Pow(x, n) | 2020/7/16 | Math | Naive-O(n), Recursive-O(logn), Recursive 2-O(logn), Bit Manipulation-O(logn) | - | |
| 👍 051 | Hard * | N-Queens | 2020/7/25 | Array | Naive-O(n^2) | testcase, TODO: another slash way in isValid | |
| 052 | Hard | N-Queens II | 2020/7/25 | Array | Naive-O(n^2) | testcase, TODO: another slash way in isValid | |
| 👍 053 | Medium * | Maximum Subarray | 2018/6/12 | Array | BruteForce-O(n³), DP-O(n), DC-O(nlogn) | Note | Do it again |
| 👍 055 | Medium * | Jump Game | 2020/4/26 | Array | Backtracking-O(2^n), DPTopDown-O(n^2), DPBottomUp-O(n^2), Greedy-O(n), Greedy2-O(n) | Note, Good DP walk through | - |
| 056 | Medium * | Merge Intervals | 2020/2/21 | Array | Sorting-O(nlogn), Naive-O(nlogn), Better-O(nlogn), Graph-O(n^2), Tree(Stream)-O(n^2) | Note | Improve performance |
| 058 | Easy | Length of Last Word | 2019/9/30 | String | Not even worth to do | - | - |
| 👍 060 | Medium * | Permutation Sequence | 2020/6/20 | String | Naive-O(n!), Backtracking-O(n!), Math-O(n) | do it again (math one) | |
| 061 | Medium * | Rotate List | 2020/10/7 | Linked List | Naive-O(n) | do it again with better way | |
| 👍 062 | Medium | Unique Paths | 2020/6/29 | Array | Naive-O(m+n), DP-O(mn) | - | |
| 👍 064 | Medium * | Minimum Path Sum | 2020/4/19 | Array | Backtracking-O(n!), Backtracking With Early Stop-O(n!), Naive-O(mn), NoAdditionalSpace-O(mn), DP-O(mn) | - | |
| 066 | Easy | Plus One | 2019/10/19 | Array | Naive-O(n), FullAdder | - | - |
| 067 | Easy | Add Binary | 2019/10/22 | String | Adder-O(n), Iterative-O(n), Python-O(n) | - | - |
| 069 | Easy | Sqrt(x) | 2019/10/22 | Search | Naive-O(n), BinarySearch-O(logn), BinarySearch2-O(logn), BinarySearch3-O(logn) | - | - |
| 070 | Easy | Climbing Stairs | 2018/6/13 | DP | DP-O(n), Recursive-O(n), Naive-O(n) | Note | - |
| 071 | Medium | Simplify Path | 2019/12/17 | String | Stack-O(n), Naive-O(n), Stack 2-O(n) | Note | - |
| 👍 072 | Hard * | Edit Distance | 2019/10/30 | String | DP-O(mn), Recursive-O(mⁿ), Recursive With Memory-O(mn), DP 2-O(mn) | Note, Baidu Interview | - |
| 👍 075 | Medium | Sort Colors | 2020/6/11 | Array | CountingSort-O(n), DutchNationalFlagProblem-O(n) | Note | testcase, do this again |
| 👍 078 | Medium | Subsets | 2018/6/27 | BM | Binary-O(2ⁿ), DFSBased-O(2ⁿ), Backtracking-O(2ⁿ), Naive-O(2ⁿ) | Note | - |
| 👍 079 | Medium * | Word Search | 2020/6/30 | Array | Naive-O(nlogn), DFS-O(mn4^s), DFS 2-O(mn4^s) | do it again | |
| 082 | Easy | Remove Duplicates from Sorted List II | 2021/1/5 | Linked List | Naive-O(n) | - | testcase |
| 083 | Easy | Remove Duplicates from Sorted List | 2019/10/22 | Linked List | Naive-O(n) | - | - |
| 085 | Hard | Maximal Rectangle | TODO | Array | Note | - | |
| 👍 086 | Medium * | Partition List | 2020/10/18 | Linked List | TwoPointer-O(n) | Naive-O(n) Fix WA, do it again | |
| 088 | Easy | Merge Sorted Array | 2019/11/18 | Array | Naive-O(n) | - | |
| 089 | Medium | Gray Code | 2021/7/1 | BitManipulation | Direct Ordering-O(n), Mirror Ordering-O(n), Binary to Gray-O(n), Better-O(n) | - | |
| 091 | Medium * | Decode Ways | 2020/9/28 | Array | Naive-O(n), | Note | TODO: Top-down DP, Bottom-up DP, Constant Space DP |
| 👍 092 | Medium * | Reverse Linked List II | 2020/9/20 | Linked List | Naive-O(n), OnePass-O(n) | testcase, OnePass | |
| 093 | Medium | Restore IP Addresses | 2019/12/16 | Array | Backtracking-O(nlogn) | Note | - |
| 094 | Medium | Binary Tree Inorder Traversal | 2018/5/29 | Binary Tree | Recursive-O(n), Iterative-O(n) | Note | - |
| 👍 096 | Medium * | Unique Binary Search Tree | 2020/6/24 | Binary Tree | Naive-O(n) | testcase, do it again | |
| 098 | Medium ▼ | Validate Binary Search Tree | 2018/6/25 | BST | Inorder-O(n), DFS-O(n) | Note | - |
| 👍 100 | Easy | Same Tree | 2019/11/20 | Binary Tree | Recursive-O(n), Naive-O(n) | Note | - |
| 👍 101 | Easy ▲ | Symmetric Tree | 2018/6/8 | Binary Tree | Recursive-O(n), Iterative-O(n), Preorder-O(n), Iterative BFS-O(n) | Note | - |
| 102 | Medium * | Binary Tree Level Order Traversal | 2018/6/7 | Binary Tree | BFS-O(n) | Note | - |
| 👍 103 | Medium * | Binary Tree Zigzag Level Order Traversal | 2020/7/22 | Binary Tree | Naive-O(n), Without Reverse-O(n), BFS-O(n), Without Reverse 2-O(n) | Need to be able to right without reverse version solution | - |
| 👍 104 | Easy | Maximum Depth of Binary Tree | 2018/6/8 | Binary Tree | Top-Down-O(n), Bottom-up-O(n), Top-Down2-O(n) | Note | - |
| 105 | Medium * | Construct Binary Tree from Preorder and Inorder Traversal | 2018/6/9 | Binary Tree | DC-O(n) | Note | - |
| 106 | Medium * | Construct Binary Tree from Inorder and Postorder Traversal | 2018/6/8 | Binary Tree | DC-O(n), Naive-O(n), Naive 2-O(nlogn), Naive 3-O(n), Better-O(n) | Note | test cases |
| 107 | Easy | Binary Tree Level Order Traversal II | 2019/12/30 | Binary Tree | Naive-O(n), Naive 2-O(n) | - | - |
| 108 | Easy | Convert Sorted Array to Binary Search Tree | 2019/11/13 | BST | Recursive-O(n) | Note | Test case |
| 👍 109 | Medium | Convert Sorted List to Binary Search Tree | 2019/11/13 | BST | Array Recursive-O(n), Two Pointer Recursive-O(nlogn) | Note | In-order Simulation, test case |
| 👍 110 | Easy ▲ | Balanced Binary Tree | 2019/12/30 | Binary Tree | Naive-O(n), Better Recursive-O(n), Postorder Iterative-O(n) | Note | do it again |
| 111 | Easy | Minimum Depth of Binary Tree | 2019/11/1 | Binary Tree | BFS-O(n) | - | |
| 👍 112 | Easy | Path Sum | 2018/6/8 | Binary Tree | Naive-O(n) | Note | Can be improved |
| 👍 113 | Medium | Path Sum II | 2019/12/23 | Binary Tree | Naive-O(n) | Note | - |
| 👍 114 | Medium | Flatten Binary Tree to Linked List | 2019/12/31 | Binary Tree | Naive-O(n) | Note | more elegant way |
| 116 | Medium | Populating Next Right Pointers in Each Node | 2019/12/24 | Binary Tree | Naive-O(n), DFS-O(n) | Note | Test |
| 117 | Medium * | Populating Next Right Pointers in Each Node II | 2019/12/24 | Binary Tree | DFS-O(n) | Note | Test |
| 118 | Easy | Pascal's Triangle | 2019/12/28 | Array | Naive-O(n) | Note | Faster approach (memory, recursive, iterative) |
| 120 | Medium * | Triangle | 2019/12/25 | Array | Naive-O(n) | Note | less space DP |
| 👍 121 | Easy * | Best Time to Buy and Sell Stock | 2018/6/13 | Array | BruteForce-O(n), Not so DP-O(n), Naive-O(n), DP-O(n) | Note | - |
| 👍 122 | Medium * | Best Time to Buy and Sell Stock II | 2018/6/14 | Array | Greedy-O(n), Tricky-O(n), Max-O(n), Greedy 2-O(n) | Note | - |
| 👍 124 | Hard * | Binary Tree Maximum Path Sum | 2020/4/29 | BinaryTree | Recursive-O(nlogn) | - | testcase, do this again |
| 125 | Easy | Valid Palindrome | 2020/1/2 | String | Naive-O(n), Naive2 (Two Pointer)-O(n) | - | - |
| 127 | Medium * | Word Ladder | 2019/12/25 | String | BFS-O(n) | Note | Bidirectional BFS |
| 👍 128 | Hard ▼ | Longest Consecutive Sequence | 2019/11/14 | Array | Naive-O(n²), HT-O(n) | Note | - |
| 👍 129 | Medium * | Sum Root to Leaf Numbers | 2020/6/26 | Binary Tree | Naive-O(nlogn), DFS-O(n), Stack DFS-O(n) | - | |
| 👍 130 | Medium | Surrounded Regions | 2020/3/25 | Array | Naive-O(mn), Boarders-O(mn) | Note | |
| 👍 133 | Medium * | Clone Graph | 2020/1/16 | Graph | Recursive-O(n), DFS-O(n), BFS-O(n) | Note, Learn: Queue & Stack | Do it again |
| 👍 135 | Hard * | Candy | 2021/6/27 | Array | Brute Force-O(n^2), Two arrays-O(n) | do it again | |
| 👍 136 | Easy | Single Number | 2019/12/18 | Array | HT-O(n), Set-O(n), BM-O(n) | Note | - |
| 👍 137 | Medium * | Single Number II | 2020/6/22 | Array | Naive-O(n), BitManipulation-O(n), BitManipulation 2-O(n), Math-O(n) | testcase | |
| 👍 138 | Medium * | Copy List with Random Pointer | 2021/2/10 | Linked List | Naive-O(n), Interweave-O(n), Map-O(n) | testcase | |
| 👍 139 | Medium | Word Break | 2020/1/14 | String | Recursive-O(2ⁿ), DP-O(n²) | Note | - |
| 👍 140 | Hard * | Word Break II | 2020/7/30 | String | Naive-O(n^2), Backtracking-O(n^2), DP | Bottom up DP | |
| 141 | Easy * | Linked List Cycle | 2019/12/31 | Linked List | Naive-O(n), TwoPointer-O(n), TwoPointer 2-O(n) | Note, Microsoft | testcase |
| 👍 142 | Medium * | Linked List Cycle II | 2020/6/25 | Linked List | Naive-O(n), Two Pointer-O(n), Two Pointer 2-O(n) | testcase | |
| 143 | Medium | Reorder List | 2020/3/25 | Linked List | Naive-O(n), Improved-O(n) | testcase | |
| 144 | Medium | Binary Tree Preorder Traversal | 2018/5/29 | Binary Tree | Recursive-O(n), Iterative-O(n) | Note | - |
| 145 | Hard ▼ | Binary Tree Postorder Traversal | 2018/6/2 | Binary Tree | Recursive-O(n), Iterative-O(n) | Note | - |
| 👍 146 | Medium * | LRU Cache | 2018/6/25 | Design | Naive, Queue, OrderedDict, DoubleLinkedList | Note | |
| 147 | Medium | Insertion Sort List | 2020/1/14 | Linked List | Naive-O(n²) | Note | Do it again with other style |
| 👍 148 | Medium * | Sort List | 2020/5/11 | Linked List | MergeSort-O(nlogn) | Do it again, InsertionSort-O(n^2), testcase | |
| 👍 150 | Medium | Evaluate Reverse Polish Notation | 2020/2/10 | Array | Stack-O(n) | Note, Learn: Queue & Stack | - |
| 151 | Medium * | Reverse Words in a String | 2019/10/22 | String | Pythonic-O(n), Trick-O(n), Naive-O(n), C Style-O(n) | Note, Microsoft Intern Interview | - |
| 👍 153 | Medium * | Find Minimum in Rotated Sorted Array | 2020/7/25 | Array | Naive-O(logn), Simpler-O(logn) | - | |
| 👍 154 | Hard * | Find Minimum in Rotated Sorted Array II | 2020/7/25 | Array | Naive-O(logn), YetAnother-O(logn) | - | |
| 155 | Easy | Min Stack | 2018/6/28 | Design | Naive, Improve | Note, Learn: Queue & Stack | Naive?! |
| 165 | Medium ▼ | Compare Version Numbers | 2023/2/23 | String | Naive-O(n), Improve-O(n) | Follow up: Sort an Array of Version Numbers, testcase | |
| 167 | Easy | Two Sum II - Input array is sorted | 2019/9/18 | Array | TwoPointer-O(n) | Note | - |
| 169 | Easy | Majority Element | 2020/5/6 | Array | Naive-O(n), Counter-O(n), Sorting-O(nlogn), Boyer-Moore Voting-O(n) | - | |
| 👍 174 | Hard * | Dungeon Game | 2020/6/21 | Array | DP-O(mn) | do it again, TODO: bottom-up dp with binary search | |
| 189 | Easy * | Rotate Array | 2018/6/14 | Array | NaiveInPlace-O(k), ExtraArray-O(n), Simplest-O(n) | Note | TODO: (Try all other solutions e.g. Cyclic Replacements / Reverse) |
| 190 | Easy | Reverse Bits | 2020/7/12 | Bit Manipulation | Naive-O(n), String-O(n) | testcase | |
| 191 | Easy | Number of 1 Bits | 2021/2/1 | Bit Manipulation | Naive-O(n) | testcase, other alternatives | |
| 198 | Easy | House Robber | 2018/6/14 | DP | DP-O(n) | Note | Can improve space complexity |
| 👍 199 | Medium * | Binary Tree Right Side View | 2021/2/6 | Binary Tree | Naive-O(n) | testcase | |
| 👍 200 | Medium * | Number of Islands | 2019/7/5 | Search | BFS-O(n²), DFS-O(n²) | Note, Learn: Queue & Stack, Dropbox OA | try Union, do this again |
| 👍 201 | Medium | Bitwise AND of Numbers Range | 2020/4/23 | BitManipulation | Naive-O(n), Improve-O(n) | ||
| 202 | Easy | Happy Number | 2020/4/2 | Math | Naive-O(n) | testcase | |
| 203 | Easy | Remove Linked List Elements | 2020/3/18 | Linked List | Dummy Node-O(n), Single Pointer-O(n) | testcase | |
| 👍 204 | Easy * | Count Primes | TODO | Math | - | ||
| 206 | Easy | Reverse Linked List | 2018/6/26 | Linked List | Iterative-O(n), Recursive-O(n) | Note, Microsoft Intern Interview | - |
| 👍 207 | Medium * | Course Schedule | 2020/7/18 | Array | CycleDetect-O(n), TopologicalSort-O(n), DFS-O(n) | - | do it again |
| 👍 208 | Medium * | Implement Trie (Prefix Tree) | 2018/6/24 | Design | Naive, Trie (using Hashmap), Trie (using Array), Trie 2 (using Hashmap) | Note | testcase |
| 👍 210 | Medium * | Course Schedule II | 2020/7/18 | Array | TopologicalSort-O(n) | - | do it again, testcase |
| 👍 211 | Medium * | Add and Search Word - Data structure design | 2020/6/30 | Design | Naive, RegEx, Tire, Self as Tire | Note | do it again |
| 👍 212 | Hard * | Word Search II | 2020/6/30 | Array | Tire-O(n) | - | do it again, testcase |
| 👍 214 | Hard * | Shortest Palindrome | ~~2020/11/8~~ TODO | String | Note | ||
| 👍 215 | Medium * | Kth Largest Element in an Array | 2019/9/18 | Array | Naive-O(nlogn), QuickSort-O(n), SelectionSortLike-O(nk), Heap (Cheat)-O(nlogn), Heap-O(nlogn), QuickSelect-O(n), QuickSelect2-O(n) | Note, Microsoft Intern Interview | do it again |
| 👍 216 | Medium * | Combination Sum III | TODO | Math | Naive-O(n) | DP | |
| 👍 221 | Medium * | Maximal Square | 2020/4/27 | Array | Naive-O(k x (mn)^2), BruteForce-O((mn)^2), DP-O(mn), Better DP-O(mn) | Note | - |
| 👍 221 | Medium * | Count Complete Tree Nodes | 2020/6/23 | Binary Tree | Naive-O(n), BinarySearch-O(n) | testcase | |
| 225 | Easy | Implement Stack using Queues | 2018/6/24 | Design | Two Queue | Note, Learn: Queue & Stack | - |
| 👍 226 | Easy * | Invert Binary Tree | 2020/6/1 | Binary Tree | Naive-O(n), Recursive-O(n) | Note | testcase |
| 👍 230 | Medium * | Kth Smallest Element in a BST | 2020/5/20 | Binary Tree | Naive-O(n), Iterative-O(n) | Note | testcase |
| 231 | Easy * | Power of Two | 2020/6/8 | Math | Naive-O(n), String-O(n), BitManipulation-O(n) | do it again | |
| 232 | Easy | Implement Queue using Stacks | 2018/6/24 | Design | Two Stack | Note, Learn: Queue & Stack | - |
| 👍 235 | Easy | Lowest Common Ancestor of a Binary Search Tree | 2021/6/30 | Binary Tree | Naive-O(n) | - | |
| 👍 236 | Medium | Lowest Common Ancestor of a Binary Tree | 2018/6/10 | Binary Tree | Naive-O(n), Naive 2-O(n), Elegant-O(n) | Note, Naive 3-O(n) | testcase |
| 237 | Easy | Delete Node in a Linked List | 2020/3/18 | Linked List | Naive-O(1) | (not worth to do) | testcase.. |
| 👍 238 | Medium * | Product of Array Except Self | 2020/3/18 | Array | ProductList-O(n), ProductList2-O(n), SingleProductList-O(n) | Note | do it again |
| 👍 239 | Hard * | Sliding Window Maximum | TODO | Array | Naive-O(n^2), Monotonic Queue-O(n) | Dropbox 2021 Summer Intern OA, Facebook 2021 Summer Intern OA | do this now!! |
| 242 | Easy | Valid Anagram | 2021/2/11 | String | Naive-O(n) | testcase | |
| 252 | Easy * | Meeting Rooms (premium) | 2020/2/20 | Array | Sorting-O(nlogn) | Note, Facebook | - |
| 👍 253 | Medium * | Meeting Rooms II (premium) | 2020/3/14 | Array | Sorting-O(nlogn) | Note, Facebook | - |
| 👍 257 | Easy ▲ | Binary Tree Paths | 2018/6/11 | Binary Tree | Iterative-O(n), Recursive-O(n) | Note, Learn: Queue & Stack | - |
| 258 | Easy | Add Digits | 2020/3/18 | Array | Naive-O(n), Iterative-O(n), NoLoop-O(1) | Note, Microsoft, not good | - |
| 👍 259 | Medium * | 3Sum Smaller (premium) | TODO | Array | Similar with 611 | do it again | |
| 👍 260 | Medium * | Single Number III | 2020/6/22 | Array | Naive-O(n), BitManipulation-O(n) | ||
| 263 | Easy | Ugly Number | 2020/7/4 | Array | Naive-O(n) | ||
| 👍 264 | Medium * | Ugly Number II | 2020/7/4 | Array | PriorityQueue-O(n), DP-O(n) | Note, Microsoft Intern Interview | do it again |
| 268 | Easy | Missing Number | 2022/12/18 | Array | Naive-O(n^2), BitManipulation-O(n) | testcase | |
| 👍 269 | Hard * | Alien Dictionary | TODO | Array | - | ||
| 278 | Easy | First Bad Version | 2020/5/1 | Iteractive | Naive-O(logn) | testcase | |
| 274 | Medium | H-Index | 2020/6/18 | Array | CumulativeSum-O(n) | sorting-based solution see 275 | testcase, do it again |
| 275 | Medium | H-Index II | 2020/6/18 | Array | Linear-O(n), BinarySearch-O(n) | testcase, do it again | |
| 👍 279 | Medium * | Perfect Squares | 2019/8/22 | Search | BFS-O(n), Recursive-O(n!), Top-Down DP-O(n), Linear Check-O(n * sqrt(n)), Bottom-Up DP-O(n), Math-O(sqrt(n)) | Note | do it again |
| 👍 285 | Medium * | Inorder Successor in BST (premium) | ~~2020/11/2~~ TODO | Binary Tree | Iterative-O(logn) | Note | do it again, testcase |
| 👍 287 | Medium | Find the Duplicate Number | 2020/6/25 | Array | Naive-O(n^2), TwoPointer(Linked List)-O(n), Set-O(n), Linked List-O(n) | do it again, testcase | |
| 283 | Easy | Move Zeroes | 2020/4/4 | Array | Naive-O(n), TwoPointer-O(n) | - | testcase |
| 284 | Medium | Peeking Iterator | 2021/2/8 | Design | Naive-O(1) | - | testcase |
| 290 | Easy | Word Pattern | 2022/12/18 | String | HashMap-O(n) | - | - |
| 297 | Hard | Serialize and Deserialize Binary Tree | 2019/11/14 | Binary Tree | Naive-O(n), Naive2-O(n) | Note | do it again with pre-order + queue |
| 👍 300 | Medium * | Longest Increasing Subsequence | 2019/11/11 | Array | BruteForce-O(2ⁿ), MemoryRecursive-O(n²), DP-O(n²), BinarySearch-O(nlogn), DP 2-O(n²), BinarySearch 2-O(nlogn) | Note | do it again |
| 303 | Easy * | Range Sum Query - Immutable | 2021/7/3 | Design | PrefixSum-O(1) | Good for beginner | - |
| 👍 304 | Medium * | Range Sum Query 2D - Immutable | 2021/7/3 | Design | Naive-O(n^2), PrefixSum-O(1) | - | |
| 👍 309 | Medium * | Best Time to Buy and Sell Stock with Cooldown | 2020/7/29 | Array | StateMachine-O(n), DP-O(n), StateMachine_DP-O(n) | do it again | |
| 👍 310 | Medium * | Minimum Height Trees | ~~2020/11/8~~ TODO | Graph | - | ||
| 👍 315 | Hard * | Count of Smaller Numbers After Self | 2021/6/26 | Array | Binary Index Tree-O(nlogn), Merge Sort-TODO | Note | do it again |
| 👍 316 | Medium * | Remove Duplicate Letters | 2020/10/11 | String | Greedy-O(n) | do it again | |
| 👍 322 | Medium * | Coin Change | 2020/10/5 | Array | Naive-O(n), DP-O(n * a) | testcase, do it again | |
| 👍 324 | Medium * | Wiggle Sort II | 2021/2/7 | Array | Naive-O(n), Median-O(n) | testcase, DO IT AGAIN | |
| 👍 328 | Medium * | Odd Even Linked List | 2020/5/16 | Linked List | Naive-O(n) | testcase, do it again | |
| 👍 329 | Hard * | Longest Increasing Path in a Matrix | 2021/7/30 | Graph | DFS | do it again | |
| 👍 330 | Hard | Patching Array | 2020/8/17 | Array | Naive-O(n^2), Trick-O(n) | - | |
| 👍 332 | Medium * | Reconstruct Itinerary | 2020/6/28 | Array | Naive-O(n), EulerianPath-O(ElogE), DFS-O(nlogn) | do it again | |
| 338 | Easy | Counting Bits | 2022/12/18 | BitManipulation | Naive-O(n), Naive2-O(n) | - | |
| * 342 | Easy | Power of Four | 2020/8/4 | Math | Naive-O(n), Bit Manipulation-O(n), Iterative-O(n), Recursive-O(n), Math-O(n) | More bit manipulation solution | - |
| 344 | Easy | Reverse String | 2020/6/4 | String | Naive-O(n), Recursive-O(n) | Note | testcase |
| 👍 347 | Medium * | Top K Frequent Elements | 2019/9/27 | Array | Naive-O(nlogn), HT Heap-O(nlogk), HT Heap 2-O(nlogk), Quickselect-avg O(n), Bucket Sort-O(n), HT-O(n) | Note | - |
| 363 | Hard * | Max Sum of Rectangle No Larger Than K | 2021/7/3 | Array | Prefix Sum-O(m^2 n^2) | Similar problem: 325, 363, 560, 974, 1074 | do it again |
| 367 | Easy | Valid Perfect Square | 2020/5/9 | Math | Naive-O(n), BinarySearch-O(n) | - | testcase |
| 👍 368 | Medium | Largest Divisible Subset | 2020/6/13 | Array | Naive-O(n!), DP-O(n²) | Note | do it again (DP) |
| 👍 373 | Medium | Find K Pairs with Smallest Sums | 2020/5/3 | Array | Priority Queue-O(klogk), Heap Merge Sort | related to 1439 (competition) | testcase, do it again |
| 👍 376 | Medium | Wiggle Subsequence | 2020/9/13 | Array | Naive-O(n), BottomUpDP-O(n) | ||
| 👍 378 | Medium | Kth Smallest Element in a Sorted Matrix | 2021/7/7 | Array | Naive-O(n^2logn) | Note | do it again (merge sort, binary search) |
| 380 | Medium | Insert Delete GetRandom O(1) | 2020/6/12 | Design | Naive-O(1) | testcase, use HT + array | |
| 👍 382 | Medium * | Linked List Random Node | 2023/3/10 | Linked List | Naive-O(n), Weighted Sampling-O(n), ReservoirSampling-O(n) | test cases | |
| 383 | Easy | Ransom Note | 2020/5/4 | String | Naive-O(n), Better-O(n) | - | - |
| 387 | Easy | First Unique Character in a String | 2020/5/5 | String | Naive-O(n) | - | testcase |
| 392 | Easy | Is Subsequence | 2020/6/9 | String | Naive-O(n) | - | |
| 👍 402 | Medium | Remove K Digits | 2020/5/13 | String | Naive-O(n) | do this again | |
| 404 | Easy | Sum of Left Leaves | 2020/8/1 | BinaryTree | Naive-O(n) | - | testcase |
| 👍 406 | Medium * | Queue Reconstruction by Height | 2020/6/6 | Array | Sorting-O(n^2), InsertFromLowest-O(nlogn), InsertFromHighest-O(nlogn), Insert-O(n) | Note | do it again, testcase |
| 👍 413 | Medium * | Arithmetic Slices | 2021/2/18 | Array | Naive-O(n) | testcase, do it again | |
| 👍 416 | Medium * | Partition Equal Subset Sum | TODO | Array | - | ||
| 419 | Medium | Battleships in a Board | TODO | Array | |||
| 👍 426 | Medium | Convert Binary Search Tree to Sorted Doubly Linked List | TODO | BST | Need subscription | - | |
| 427 | Medium | Construct Quad Tree | TODO | Graph | Naive | ||
| 👍 430 | Medium * | Flatten a Multilevel Doubly Linked List | 2020/7/10 | Linked List | Naive-O(n), Stack-O(n) | do it again, testcase | |
| 👍 435 | Medium * | Non-overlapping Intervals | TODO | Array | |||
| 👍 438 | Medium | Find All Anagrams in a String | 2020/5/17 | String | Naive-O(n) | - | |
| 👍 441 | Easy | Arranging Coins | 2020/7/1 | Math | Naive-O(n), Binary Search-O(logn), Math-O(1) | - | |
| 443 | Medium | String Compression | 2023/3/2 | String | Naive-O(n), Two Pointer-O(n), Two Pointer 2-O(n) | - | |
| 👍 451 | Medium | Sort Characters By Frequency | 2020/5/22 | String | Naive-O(n), PQ-O(n) | testcase | |
| 👍 452 | Medium | Minimum Number of Arrows to Burst Balloons | 2020/10/10 | Array | Naive-O(nlogn) | - | |
| 460 | Hard | LFU Cache | 2018/6/25 | Design | Naive | Note | OrderedDict, LinkedList |
| 461 | Easy | Hamming Distance | 2020/7/5 | Bit Manipulation | Naive-O(n) | testcase | |
| 👍 463 | Easy | Island Perimeter | 2020/7/7 | Array | Naive-O(n), Neighbour-O(n), CountEdge-O(n), StringEdge-O(n) | testcase | |
| 👍 468 | Medium | Validate IP Address | 2020/6/16 | String | Naive-O(n) | Microsoft Intern Interview | todo: pure regex, pure rule |
| 👍 477 | Medium | Total Hamming Distance | TODO | Bit Manipulation | string approach | ||
| 👍 494 | Medium | Target Sum | 2020/2/10 | Array | Naive-O(n²), RecursiveWithMemory-O(l*n), 2D DP-O(l*n), 1D DP-O(l*n) | Note, Learn: Queue & Stack | more optimization on DP |
| 👍 518 | Medium * | Coin Change 2 | 2020/6/7 | Array | Naive-O(n^t), DP-O(nt) | Note | do it again (bottom up 1D DP) |
| 👍 502 | Hard | IPO | 2023/2/23 | Array | Naive-O(kn), SortHeap-O(nlogn), SortHeapImprove-O(nlogn) | - | |
| 👍 503 | Medium * | Next Greater Element II | TODO | Array | - | ||
| 👍 516 | Medium * | Longest Palindromic Subsequence | 2023/4/14 | String | Top-Down DP-O(n²), Bottom-Up DP-O(n²), Bottom-Up DP-O(n²) O(n) space | O(n) Space Bottom-Up DP, Follow Up (list result) | |
| 520 | Easy | Detect Capital | 2020/8/3 | String | Naive-O(n), CapitalCount-O(n), RegEx-O(n) | - | |
| 👍 525 | Medium * | Contiguous Array | 2020/4/13 | Array | Naive-O(n²), HT-O(n), ExtraArray-O(n), HT2-O(n) | Didi Interview | do it again |
| 👍 526 | Medium | Beautiful Arrangement | 2021/1/3 | Array | BruteForce-O(n!), Backtracking with Memory | testcase | |
| 528 | Medium * | Random Pick with Weight | 2020/6/5 | Design | Naive-O(n), Naive2-O(n), Binary Search-O(n), OnePass-O(n) | Note | testcase |
| 532 | Medium | K-diff Pairs in an Array | 2020/10/3 | Array | Naive-O(n^2), HT-O(n), Naive2-O(n) | testcase | |
| 👍 538 | Medium * | Convert BST to Greater Tree | 2021/2/7 | BinaryTree | DFS-O(n), Array-O(n) | same as problem 1038 | testcase, do it again |
| 👍 540 | Medium | Single Element in a Sorted Array | 2020/5/12 | Array | Naive-O(n), BinarySearchXOR-O(logn), XOR-O(n), BinarySearch-O(logn) | - | |
| 👍 543 | Easy | Diameter of Binary Tree | 2020/4/11 | Binary Tree | DFS-O(n) | testcase | |
| 👍 547 | Medium | Friend Circles | ~~2020/11/2~~ TODO | Graph | DFS-O(n) | testcase | |
| 559 | Easy | Maximum Depth of N-ary Tree | 2019/11/14 | Tree | Naive-O(n), DFS-O(n) | Note | Test |
| 👍 560 | Medium | Subarray Sum Equals K | 2020/4/22 | Array | BruteForce-O(n^3), CumulativeSum-O(n²), WithoutSpace-O(n²), HT-O(n) | Note | |
| 563 | Easy | Binary Tree Tilt | 2020/8/1 | BinaryTree | Naive-O(n) | iterative | |
| 566 | Easy | Reshape the Matrix | 2021/7/5 | Array | Naive-O(n) | Good for beginner | - |
| 👍 567 | Medium | Permutation in String | 2020/5/18 | String | Naive-O(n) | testcase | |
| 594 | Easy | Longest Harmonious Subsequence | 2021/2/4 | Array | Naive-O(n) | testcase | |
| 605 | Easy | Can Place Flowers | 2023/3/20 | Array | Naive-O(n) | - | |
| 👍 611 | Medium * | Valid Triangle Number | 2023/2/28 | Array | Brute Force-O(n^3), Binary Search-O(n^2logn), Binary Search 2-O(n^2logn), Two Pointer-O(n^2) | Byte Dance Interview | - |
| 👍 621 | Medium * | Task Scheduler | 2020/7/28 | Array | Naive-O(nlogn) | Note | TODO: more solution |
| 👍 624 | Easy | Maximum Distance in Arrays (premium) | 2020/10/5 | Array | Naive-O(n) | - | |
| 👍 630 | Hard * | Course Schedule III | 2021/5/2 | Array | Naive-O(n+1!), Top-down DP-O(n * d), PriorityQueue-O(nlogn) | - | topological sort, Do it again (all total official has 6) |
| 👍 636 | Medium * | Exclusive Time of Functions | 2020/10/25 | Ad Hoc | Naive-O(n) | testcase | |
| 639 | Hard | Decode Ways II | TODO | String | - | - | |
| 👍 658 | Medium * | Find K Closest Elements | 2021/7/2 | Array | Naive-O(logn + k), Double Sort-O(nlogn + klogk), Find Left Bound-O(log(n - k) + k) | Note | do it again |
| 👍 662 | Medium * | Maximum Width of Binary Tree | 2020/7/9 | Binary Tree | Naive-O(n) | Note | do it again (more elegant way), testcase |
| 👍 665 | Medium * | Non-decreasing Array | 2021/5/4 | Array | Greedy-O(n) | Note | do it again |
| 👍 669 | Medium * | Trim a Binary Search Tree | 2021/2/2 | Binary Tree | Recursive DFS-O(n) | do it again | |
| 👍 673 | Medium | Number of Longest Increasing Subsequence | TODO | String | |||
| 👍 674 | Easy | Longest Continuous Increasing Subsequence | TODO | String | |||
| 👍 678 | Medium * | Valid Parenthesis String | 2020/4/17 | String | Naive-O(n*3^n), DP-O(n), Greedy-O(n) | Note | do this again |
| 👍 684 | Medium * | Redundant Connection | 2020/10/18 | Graph | DSU-O(n), Union Find-O(n), Cycle Prevention DFS-O(n^2) | do this again, DFS, BFS | |
| 👍 685 | Hard | Redundant Connection II | TODO | Graph | DSU | ||
| 687 | Medium | Longest Univalue Path | TODO | Binary Tree | Note | - | |
| 👍 698 | Medium * | Partition to K Equal Sum Subsets | TODO | Array | - | ||
| 700 | Easy | Search in a Binary Search Tree | 2020/6/15 | Binary Tree | Naive-O(logn) | testcase | |
| 701 | Medium * | Insert into a Binary Search Tree | 2020/10/6 | Binary Tree | Naive-O(logn), Recursive-O(logn) | testcase | |
| 703 | Easy | Kth Largest Element in a Stream | 2020/5/8 | Design | Naive-O(n), Heap-O(n) | Note | testcase |
| 704 | Easy * | Binary Search | 2020/5/9 | Array | Iterative(<)-O(logn), Recursive(<=)-O(logn), Bisect-O(logn) | - | |
| 705 | Easy | Design HashSet | 2020/8/2 | Design | Naive-O(1), With Hash Function-O(1) | testcase | |
| 733 | Easy | Flood Fill | 2020/5/11 | Array | Naive-O(n), DFS-O(n), IterativeDFS-O(n) | Test | |
| 👍 718 | Medium * | Maximum Length of Repeated Subarray | 2021/7/8 | Array | DP-O(mn) | Binary Search with Rolling Hash | |
| 👍 739 | Medium | Daily Temperatures | 2019/12/26 | Array | Naive-O(n²), Naive2-O(n²), Stack-O(n²) | Note, Learn: Queue & Stack | - |
| 👍 745 | Hard * | Prefix and Suffix Search | 2021/5/1 | Design | RegularExpression-O(n), Paired Trie-O(nk^2 + qk), Trie of Suffix Wrapped Words-O(nk^2 + qk), Double Set-O(n) | - | |
| 👍 752 | Medium | Open the Lock | 2019/7/5 | Search | BFS-O(n²) | Note, Learn: Queue & Stack | Improve time complexity |
| 👍 763 | Medium | Partition Labels | 2020/9/5 | String | Naive-O(n) | ||
| 771 | Easy | Jewels and Stones | 2020/5/2 | String | Naive-O(n), Naive2-O(n) | testcase | |
| 👍 783 | Easy | Minimum Distance Between BST Nodes | 2023/2/17 | Binary Tree | DFS-O(n), DFS Improve-O(n) | testcase | |
| 👍 784 | Easy | Letter Case Permutation | 2020/7/25 | String | Naive-O(n), Naive 2-O(n) | testcase, more elegant way | |
| 👍 785 | Medium * | Is Graph Bipartite? | 2021/2/14 | Graph | Color-O(V + E) | do it again | |
| 👍 787 | Medium * | Cheapest Flights Within K Stops | 2020/6/14 | Graph | Naive-O(V + E), BFS-O(V + E), DP-O(n²), Naive 2-O(V + E) | Note | do it again |
| 790 | Medium | Domino and Tromino Tiling | TODO | Geometry | - | ||
| 👍 797 | Medium * | All Paths From Source to Target | 2020/7/24 | Graph | Naive-O(2^n), Backtracking-O(2^n) | do it again, TODO: iterative version | |
| 801 | Medium | Minimum Swaps To Make Sequences Increasing | TODO | Array | Note | - | |
| 821 | Easy | Shortest Distance to a Character | 2021/2/7 | String | Naive-O(n) | testcase, more simple approach | |
| 829 | Hard * | Consecutive Numbers Sum | TODO | Math | - | ||
| 👍 834 | Hard * | Sum of Distances in Tree | 2022/12/22 | Graph | Naive-O(n^3), SubtreeSumAndCount-O(n) | do it again | |
| 841 | Medium | Keys and Rooms | 2022/12/20 | Graph | DFS-O(n), DFS (loop)-O(n) | ||
| 842 | Medium | Split Array into Fibonacci Sequence | 2022/12/20 | String | Backtracking-O(n^m) | ||
| 844 | Easy | Backspace String Compare | 2020/4/9 | String | Naive-O(n), TwoPointer-O(n) | testcase | |
| 👍 857 | Hard * | Minimum Cost to Hire K Workers | 2020/3/17 | Array | Greedy-O(n²logn), Greedy w/ PQ-O(nlogn) | Note | - |
| 859 | Easy | Buddy Strings | 2020/10/12 | String | Naive-O(n^2), Naive-O(n), Better-O(n) | testcase | |
| 👍 862 | Hard * | Shortest Subarray with Sum at Least K | TODO | Array | Note | Deque | |
| 867 | Easy | Transpose Matrix | 2023/2/21 | Array | Naive-O(n), Numpy-O(n) | testcase | |
| 👍 870 | Medium | Advantage Shuffle | 2023/3/9 | Array | Sorting-O(n), SortingWithQueue-O(n) | - | |
| 👍 875 | Medium | Koko Eating Bananas | 2020/8/8 | Array | Naive-O(n), BinarySearch-O(n) | testcase | |
| 876 | Easy | Middle of the Linked List | 2020/4/8 | Linked List | Naive-O(n), TwoPointer-O(n) | testcase | |
| 👍 881 | Medium ▼ | Boats to Save People | 2023/4/3 | Array | Greedy-O(nlogn) | Follow up: 1986 | |
| 👍 886 | Medium * | Possible Bipartition | 2022/12/21 | Graph | BFS-O(n) | do it again | |
| 👍 901 | Medium | Online Stock Span | 2020/5/19 | Design | Naive-O(n), Count-O(n), Stack-O(n) | testcase | |
| 👍 904 | Medium | Fruit Into Baskets | 2023/2/7 | Array | Naive-O(n^2), SlidingWindow-O(n), Greedy-O(n) | - | |
| 👍 912 | Medium * | Sort an Array | 2023/3/1 | Array | Quick Sort-O(nlogn), 3-Way Quick Sort-O(nlogn) TODO, Heap Sort-O(nlogn), Heap Sort 2-O(nlogn) TODO, Merge Sort-O(nlogn) TODO | Implement all other O(nlogn) sorting algorithms | |
| 916 | Medium | Word Subsets | 2020/9/5 | String | Naive-O(n), Improve-O(n), Counter-O(n) | testcase | |
| 👍 918 | Medium * | Maximum Sum Circular Subarray | 2020/5/15 | Array | Naive-O(n), MinSumSubarray-O(n) | Note | use other approach |
| 933 | Easy | Number of Recent Calls | 2020/10/1 | Design | Naive-O(n), Naive2-O(n), BinarySearch-O(logn), Queue-O(min(n, 3000)), CircularList-O(n) | testcase | |
| 944 | Easy | Delete Columns to Make Sorted | 2023/1/3 | Array | Naive-O(n) | testcase | |
| 👍 946 | Medium * | Validate Stack Sequences | 2023/4/13 | Array | Naive-O(n), Better-O(n) | - | |
| 👍 952 | Hard * | Largest Component Size by Common Factor | TODO | Array | |||
| 957 | Medium | Prison Cells After N Days | 2020/7/3 | Array | Naive-O(n), LoopDetection-O(n) | Note-O(n) | |
| 👍 958 | Medium * | Check Completeness of a Binary Tree | 2023/3/15 | Binary Tree | BFS-O(n), DFS-O(n) | Note | testcase, do it again |
| 👍 978 | Medium | Longest Turbulent Subarray | TODO | String | |||
| 👍 986 | Medium * | Interval List Intersections | 2020/5/23 | Array | TwoPointer-O(m+n) | Note | testcase, do it again |
| 989 | Easy | Add to Array-Form of Integer | 2023/2/15 | Array | Naive-O(n), Python-O(n) | - | |
| 993 | Easy | Cousins in Binary Tree | 2020/5/7 | Binary Tree | IterativeBFS-O(n), RecursiveDFS-O(n) | testcase | |
| 👍 994 | Medium | Rotting Oranges | 2020/9/13 | Graph | Naive-O(V + E) | ||
| 997 | Easy | Find the Town Judge | 2020/5/10 | Array | Naive-O(n), Improve-O(n), Naive 2-O(n) | ||
| 👍 1004 | Medium | Max Consecutive Ones III | 2021/6/29 | Array | Naive-O(n) | - | |
| 1008 | Medium | Construct Binary Search Tree from Preorder Traversal | 2020/4/20 | Binary Tree | Naive-O(n), Naive2-O(n) | Note | testcase |
| 1009 | Easy | Complement of Base 10 Integer (Number Complement) | 2020/5/4 | BitManipulation | Naive-O(n), Pythonic-O(n), BM-O(n) | testcase | |
| 👍 1010 | Easy | Pairs of Songs With Total Durations Divisible by 60 | 2020/10/14 | Array | Naive-O(n^2), PrefixSum-O(n) | testcase | |
| 👍 1011 | Medium * | Capacity To Ship Packages Within D Days | 2023/2/22 | Array | BinarySearch-O(nlogn), BinarySearchImprove-O(nlogn) | - | |
| 👍 1020 | Medium * | Number of Enclaves | 2023/4/7 | Graph | DFS-O(n), Simpler DFS-O(n) | - | |
| 👍 1029 | Easy * | Two City Scheduling | 2020/6/3 | Array | Naive-O(nlogn), Diff-O(nlogn), Sorting-O(nlogn) | - | |
| 👍 1035 | Medium * | Uncrossed Lines | 2020/5/25 | Array | Naive-O(mn), BottomUpDP-O(mn) | Note | testcase, do it again |
| 👍 1041 | Medium | Robot Bounded In Circle | 2020/11/8 | Array | Naive-O(n) | LinkedIn 2021 Summer Intern | testcase, do it again |
| 👍 1044 | Hard | Longest Duplicate Substring | 2020/6/20 | String | Naive, BinarySearch-RabinKarp-O(nlogn) | Note | do it again |
| 👍 1046 | Easy * | Last Stone Weight | 2020/4/12 | Array | Heap-O(nlogn), Insertion Sort-O(nlogn) | - | |
| 1047 | Easy | Remove All Adjacent Duplicates In String | 2021/6/28 | String | Naive-O(n^n), Stack-O(n), Remove Pattern-O(n) | Good for beginner | - |
| 1071 | Easy | Greatest Common Divisor of Strings | 2022/12/19 | String | Naive-O(n^n) | - | |
| 👍 1091 | Medium | Shortest Path in Binary Matrix | 2021/2/13 | Graph | Naive-O(n) | testcase | |
| 👍 1094 | Medium | Car Pooling | 2020/9/21 | Array | Naive-O(n) | testcase | |
| 👍 1143 | Medium * | Longest Common Subsequence | 2019/10/22 | String | Brute Force-O(2ⁿ), DP-O(mn) | Note, Baidu Interview (twice), DP + Return Subsequence | Follow Up: Nowcoder - Return Subsequence Itself |
| 👍 1200 | Medium * | Minimum Absolute Difference | TODO | Array | - | ||
| 👍 1220 | Hard * | Count Vowels Permutation | 2021/7/4 | Math | DP-O(5n) | good DP problem | do it again |
| 1232 | Easy | Check If It Is a Straight Line | 2020/5/8 | Math | Naive-O(n) | - | |
| 👍 1249 | Medium * | Minimum Remove to Make Valid Parentheses | 2020/7/25 | String | Naive-O(n), Naive 2-O(n) | stack, follow up: O(1) space | |
| 👍 1277 | Medium | Count Square Submatrices with All Ones | 2020/5/22 | String | Naive-O(n^4), DP-O(mn) | testcase, do it again | |
| 👍 1288 | Medium | Remove Covered Intervals | 2020/10/4 | Array | Naive-O(n) | testcase, do it with other solution | |
| 👍 1291 | Medium | Sequential Digits | 2019/12/15 | Math | Naive-O(n), Recursive-O(nlogn), Naive2-O(n) | Weekly Contest 167 | testcase, Naive2 (generate sequence in order) |
| 👍 1293 | Hard * | Shortest Path in a Grid with Obstacles Elimination | 2021/7/30 | Graph | BFS | Google Interview | do it again |
| 👍 1319 | Medium | Number of Operations to Make Network Connected | 2023/3/23 | Graph | DFS-O(n) | union find | |
| 👍 1312 | Hard * | Minimum Insertion Steps to Make a String Palindrome | 2023/4/22 | String | Top-Down DP-O(n), Bottom-Up DP-O(n) (TODO) | Similar to 72 Edit Distance | Bottom-Up DP (see faster solution) |
| 👍 1337 | Easy | The K Weakest Rows in a Matrix | 2021/2/15 | Array | Naive-O(n) | max heap + binary search | |
| 1338 | Medium ▼ | Reduce Array Size to The Half | 2021/7/6 | Array | Naive-O(n) | - | |
| 1342 | Easy | Number of Steps to Reduce a Number to Zero | 2020/9/20 | Math | Naive-O(n), Naive 2-O(n) | Good for beginner | - |
| 👍 1344 | Medium | Angle Between Hands of a Clock | 2020/7/14 | Math | Naive-O(1) | - | |
| 1379 | Medium ▼ | Find a Corresponding Node of a Binary Tree in a Clone of That Tree | 2021/1/2 | Binary Tree | Naive-O(n) | Note | testcase, iterative |
| 👍 1425 | Hard | Constrained Subsequence Sum | TODO | Array | Note | - | |
| 👍 1444 | Hard | Number of Ways of Cutting a Pizza | 2023/3/31 | Array | Prefix Sum + DP-O(kmn) | do it again | |
| 👍 1462 | Medium * | Course Schedule IV | TODO | Array | - | - | - |
| 👍 1466 | Medium * | Reorder Routes to Make All Paths Lead to the City Zero | 2023/3/25 | Graph | BFS-O(n) | - | do it again (faster), testcases |
| 1472 | Medium | Design Browser History | 2023/3/18 | Design | Naive | testcase | |
| 👍 1475 | Easy * | Final Prices With a Special Discount in a Shop | 2020/8/22 | Array | Naive-O(n^2), TwoPointer-O(n^2), Stack-O(n^2) | - | |
| 👍 1480 | Easy * | Running Sum of 1d Array | 2021/5/3 | Array | Naive-O(n), In-Place-O(n) | - | |
| 1491 | Easy | Average Salary Excluding the Minimum and Maximum Salary | 2023/5/1 | Array | Naive-O(n) | Good for beginner | - |
| 👍 1539 | Easy | Kth Missing Positive Number | 2021/1/6 | Array | Naive-O(n+k), Naive 2-O(n), Binary Search-O(logn), Bisect-O(logn) | - | - |
| 👍 1547 | Hard | Minimum Cost to Cut a Stick | 2020/10/17 | Array | Top-Down DP-O(n^2) | FreeWheel 2020 OA, Weekly Contest 201 | testcase |
| 👍 1572 | Easy | Matrix Diagonal Sum | 2023/5/8 | Array | Naive-O(n), Better-O(n) | - | - |
| 👍 1601 | Hard | Maximum Number of Achievable Transfer Requests | 2020/9/28 | Graph | Naive-O(2^n) | testcase | |
| 👍 1640 | Easy | Check Array Formation Through Concatenation | 2021/1/1 | Array | DFS-O(n), HashTable-O(n) | Weekly Contest 213, Note | testcase, do it again |
| 👍 1675 | Hard | Minimize Deviation in Array | 2023/2/24 | Array | Greedy-O(nlogn) | do it again, testcase | |
| 1768 | Easy | Merge Strings Alternately | 2023/4/18 | String | Naive-O(n) | Good for beginner | - |
| 👍 1834 | Medium | Single-Threaded CPU | 2022/12/29 | Array | Greedy-O(n^2), Heap-O(nlogn), HeapImprove-O(nlogn) | - | |
| 1822 | Easy | Sign of the Product of an Array | 2023/5/2 | Math | Python-O(n), Naive-O(n), Better-O(n) | Good for beginner | - |
| 👍 1861 | Medium | Rotating the Box | TODO | AdHoc | Code Signal | - | |
| 👍 1986 | Medium * | Minimum Number of Work Sessions to Finish the Tasks | TODO | Array | - | ||
| 👍 1928 | Hard * | Minimum Cost to Reach Destination in Time | TODO | Graph | Google Interview | - | |
| 👍 1971 | Easy | Find if Path Exists in Graph | 2022/12/19 | Graph | BFS-O(n), DFS-O(n), UnionFind-O(n) | ||
| 👍 2099 | Easy | Find Subsequence of Length K With the Largest Sum | 2023/2/23 | Array | Naive-O(n) | better testcase | |
| 👍 2187 | Medium | Minimum Time to Complete Trips | 2023/3/7 | Array | Binary Search-O(nlogn) | - | |
| 👍 2244 | Medium | Minimum Rounds to Complete All Tasks | 2023/1/4 | Array | Greedy-O(n), Greedy2-O(n) | - | |
| 👍 2279 | Medium | Maximum Bags With Full Capacity of Rocks | 2022/12/27 | Array | Greedy-O(n) | - | |
| 👍 2316 | Medium *▲ | Count Unreachable Pairs of Nodes in an Undirected Graph | 2023/3/25 | Graph | Union Find (TLE) - O(n), Union Find Improve - O(n), DFS-O(n), BFS-O(n) | other solution, testcases | |
| 👍 2348 | Medium | Number of Zero-Filled Subarrays | 2023/3/21 | Array | Math-O(n), Simplified Math-O(n) | other solutions | |
| 👍 2360 | Hard * | Longest Cycle in a Graph | 2023/3/26 | Graph | DFS With Time-O(n) | topological sort | |
| 2390 | Medium ▼ | Removing Stars From a String | 2023/4/11 | String | Stack-O(n) | - | |
| 👍 2405 | Medium ▼ | Optimal Partition of String | 2023/4/4 | String | Greedy-O(n) | try other solution | |
| 👍 2439 | Medium ▲ | Minimize Maximum of Array | 2023/4/5 | Array | GreedyBruteForce-O(n*max(nums)), Binary Search-O(n*max(nums)), Prefix Sum-O(n) | - | |
| 👍 2492 | Medium | Minimum Score of a Path Between Two Cities | 2023/3/23 | Graph | BFS-O(n), Union Find-O(n) | DFS, Union-Find | |
| ??? | Easy | Counting Elements | 2020/4/7 | Array | Naive-O(nlogn), SinglePass-O(n) | testcase | |
| ??? | Easy | Perform String Shifts | 2020/4/15 | String | Naive-O(n) | testcase | |
| ??? | Easy | Leftmost Column with at Least a One | 2020/4/21 | Interactive | Naive-O(mn), BinarySearch-O(n), Improve-O(n+m) | Note | testcase, do this again |
| ??? | Easy | First Unique Number | 2020/4/28 | Design | Naive, HT Counter, Double Linked List, Double Linked List + HT | Note | testcase, improve follow the hint (set, heap) |
| ??? | Easy | Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree | 2020/4/30 | Binary Tree | Naive | testcase, improve (like iterative dfs) |
LintCode
| Number | Difficulty | Problem | Date | Category | Method-TimeComplexity | Remark | TODO |
|---|---|---|---|---|---|---|---|
| 👍 127 | Medium | Topological Sorting | 2023/2/23 | Graph | BFS-O(nlogn) | DFS, testcase, do it again | |
| 👍 391 | Medium | Number of Airplanes in the Sky | 2023/2/16 | Array | HeapSort-O(nlogn), SweepLine-O(nlogn) | Do it again |
Contest
LeetCode Learn Progress
Introduction to Algorithm
Introduction to Data Structure
- Binary Tree - note
- Queue & Stack - note
- Binary Search Tree
- Trie - note
Learning about...
| Design | Date | Category | Implementation | Remark | TODO |
|---|---|---|---|---|---|
| Circular Queue | 2019/7/4 | Data Structure | C++ | Note, Design Circular Queue | - |
| Min Stack | 2019/12/26 | Data Structure | C++ | Min Stack | - |
Notes
Data Structure
- Queue and Stack
- Binary Tree
- Priority Queue and Heap
- Trie (prefix tree)
- Fenwick Tree / Binary Indexed Tree (TODO)
- Disjoint Set (Union-Find)
Algorithm
- Dynamic Programming
- Binary Search
DIY Progress
Data Structure
| Difficulty | Problem | Date | Category | Method-TimeComplexity | Remark | TODO |
|---|---|---|---|---|---|---|
| Easy | Linked List | 2018/3/12 | Linked List | Singly-Linked List | - | Double-Linked List |
| Medium * | Binary Heap (Min/Max Heap) | 2018/6/28 | Design | Binary Heap | Note | - |
Classic Algorithm Problem
| Difficulty | Problem | Date | Category | Method-TimeComplexity | Remark | TODO |
|---|---|---|---|---|---|---|
| Easy | Linked List | 2018/3/12 | Linked List | Singly-Linked List | - | Double-Linked List |
| Medium * | Binary Heap (Min/Max Heap) | 2018/6/28 | Design | Binary Heap | Note | - |
Resources
Online Judgement / Assessment
- LeetFree
- Codeforces
- Newcoder - Interview OA in China
- CodeSignal - Intern Interview OA in America
- HackerRank - Interview OA
- binarysearch.io
- Topcoder Top Technology Talent On-Demand
Paid
Grokking Dynamic Programming Patterns for Coding Interviews
- 0/1 Knapsack
- Equal Subset Sum Partition
- Subset Sum
- Minimum Subset Sum Difference
- Count of Subset Sum
- Target Sum
- Unbounded Knapsack
- Unbounded Knapsack
- Rod Cutting
- Coin Change
- Minimum Coin Change
- Maximum Ribbon Cut
- Fibonacci Numbers (Fibonacci Sequence (eg: House Thief, Jump Game))
- Fibonacci numbers
- Staircase
- Number factors
- Minimum jumps to reach the end
- Minimum jumps with fee
- House thief
- Palindromic Subsequence (Shortest Path (eg: Unique Paths I/II))
- Longest Palindromic Subsequence
- Longest Palindromic Substring
- Count of Palindromic Substrings
- Minimum Deletions in a String to make it a Palindrome
- Palindromic Partitioning
- Longest Common Substring
- Longest Common Substring
- Longest Common Subsequence
- Minimum Deletions & Insertions to Transform a String into another
- Longest Increasing Subsequence
- Maximum Sum Increasing Subsequence
- Shortest Common Super-sequence
- Minimum Deletions to Make a Sequence Sorted
- Longest Repeating Subsequence
- Subsequence Pattern Matching
- Longest Bitonic Subsequence
- Longest Alternating Subsequence
- Edit Distance
- Strings Interleaving
0/1 Knapsack
Unbounded Knapsack
Shortest Path (eg: Unique Paths I/II)
Fibonacci Sequence (eg: House Thief, Jump Game)
Longest Common Substring/Subsequeunce
Course
- Algorithms | Coursera (Paid)
Books
- Cracking the Coding Interview
- labuladong/fucking-algorithm
LeetCode Links
2020 Coronavirus quarantine special events
- 30-Day LeetCoding Challenge - 2020/4/1~2020/4/30
- May LeetCoding Challenge
- June LeetCoding Challenge
- July LeetCoding Challenge
- August LeetCoding Challenge
- September LeetCoding Challenge
- October LeetCoding Challenge
- May LeetCoding Challenge 2021
- June LeetCode Challenge 2021
- July LeetCode Challenge 2021
Top Interview Questions
- Top Interview Questions - Easy Collection
- Top Interview Questions - Medium Collection
- Top Interview Questions - Hard Collection
LeetCode Premium Top Question List:
TOP GOOGLE QUESTION:
[1, 85, 127, 224, 315, 329, 346, 359, 394, 444, 489, 528, 552, 560, 562, 642, 652, 659, 690, 715, 722, 727, 729, 752, 753, 767, 809, 833, 843, 844, 846, 900, 946, 951, 1031, 1048, 1060, 1110, 1138, 1153, 1231, 1293, 1296, 1345, 1376, 1423, 1438, 1463, 1477, 1548]TOP MICROSOFT QUESTION:[1, 2, 5, 8, 15, 17, 23, 25, 33, 34, 41, 42, 45, 46, 49, 53, 54, 55, 79, 85, 88, 98, 102, 103, 105, 124, 127, 138, 140, 146, 151, 155, 160, 200, 210, 212, 232, 236, 240, 273, 287, 295, 297, 340, 428, 445, 468, 545, 767, 1239]TOP FACEBOOK QUESTION:[23, 34, 56, 65, 67, 88, 124, 125, 133, 140, 158, 173, 199, 211, 215, 227, 238, 249, 269, 270, 273, 278, 282, 297, 301, 311, 339, 340, 398, 415, 438, 523, 528, 543, 560, 621, 636, 670, 680, 689, 721, 785, 938, 953, 973, 986, 987, 1026, 1249, 1428]TOP AMAZON QUESTION:[1, 5, 21, 23, 42, 49, 56, 126, 127, 138, 139, 140, 146, 185, 200, 210, 212, 227, 239, 253, 269, 273, 295, 297, 348, 353, 380, 403, 437, 460, 472, 588, 682, 692, 721, 763, 815, 819, 863, 901, 937, 957, 973, 994, 1091, 1120, 1152, 1167, 1192, 1429]
Detail Solutions
- C++
- 《LeetCode題解》
- wisdompeak/LeetCode: This repository contains the solutions and explanations to the algorithm problems on LeetCode. Only medium or above are included. All are written in C++/Python and implemented by myself. The problems attempted multiple times are labelled with hyperlinks.
- Huifeng Guan - YouTube
- JavaScript
- Go
- Mixed
Study Group
- Cruel Study Group
Others
Usually online judgement TLE limitation is 1 second. => we can estimate the time complexity as the power of ten.
- Type hints (Python 3.5 feature)
- Python Operations Time Complexity
- Python Tricks
- Tkinter (
_tkinter.TclError: no display name and no $DISPLAY environment variable,_tkinter.TclError: couldn't connect to display ":0")- Install tkinter
sudo apt-get install python3-tk - Install Xming X Server for Windows
export DISPLAY=:0;
- Install tkinter
- VSCode debug
Quick typing math symbol
- MacOS
control + command + space
- Ubuntu
ctrl + .in naive GTK Linux appsctrl + shift + e + spacein some other apps- How to insert an emoji into a text in Ubuntu 18.04 and later? - Ask Ubuntu
- Emoji Selector - GNOME Shell Extensions
- GNOME Shell Extensions
- Windows
win + .
VSCode ignore formatting on file
! [new branch] master -> origin/master (unable to update local ref)
warning: url has no scheme:
fatal: credential url cannot be parsed:
error: update_ref failed for ref 'refs/remotes/origin/master': cannot lock ref 'refs/remotes/origin/master': unable to resolve reference 'refs/remotes/origin/master': reference broken