Skip to content
forked from shuboc/LeetCode-2

πŸ“ Python / C++ 11 Solutions of All LeetCode Questions

License

Notifications You must be signed in to change notification settings

zhao181/LeetCode

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

The number of LeetCode questions is increasing every week. For more questions and solutions, you can see my LintCode repository. I'll keep updating for full summary and better solutions. Stay tuned for updates. (Notes: "πŸ“–" means you need to subscribe to LeetCode premium membership for the access to premium questions.)

Algorithms

Database

Shell

Reference

Bit Manipulation

# Title Solution Time Space Difficulty Tag Note
136 Single Number C++ Python O(n) O(1) Easy
137 Single Number II C++ Python O(n) O(1) Medium
190 Reverse Bits C++ Python O(1) O(1) Easy
191 Number of 1 Bits C++ Python O(1) O(1) Easy
201 Bitwise AND of Numbers Range C++ Python O(1) O(1) Medium
231 Power of Two C++ Python O(1) O(1) Easy LintCode
260 Single Number III C++ Python O(n) O(1) Medium
268 Missing Number C++ Python O(n) O(1) Medium LintCode
318 Maximum Product of Word Lengths C++ Python O(n) ~ O(n^2) O(n) Medium Bit Manipulation, Counting Sort, Pruning
342 Power of Four C++ Python O(1) O(1) Easy
371 Sum of Two Integers C++ Python O(1) O(1) Easy LintCode
389 Find the Difference C++ Python O(n) O(1) Easy
393 UTF-8 Validation C++ Python O(n) O(1) Medium
401 Binary Watch C++ Python O(1) O(1) Easy
411 Minimum Unique Word Abbreviation C++ Python O(2^n) O(n) Hard πŸ“–
421 Maximum XOR of Two Numbers in an Array C++ Python O(n) O(n) Medium
461 Hamming Distance C++ Python O(1) O(1) Easy
462 Minimum Moves to Equal Array Elements II C++ Python O(n) on average O(1) Medium
477 Total Hamming Distance C++ Python O(n) O(1) Medium
645 Set Mismatch C++ Python O(n) O(1) Easy
693 Binary Number with Alternating Bits C++ Python O(1) O(1) Easy
762 Prime Number of Set Bits in Binary Representation C++ Python O(1) O(1) Easy
868 Binary Gap C++ Python O(1) O(1) Easy
898 Bitwise ORs of Subarrays C++ Python O(n) O(1) Medium

Array

# Title Solution Time Space Difficulty Tag Note
015 3 Sum C++ Python O(n^2) O(1) Medium Two Pointers
016 3 Sum Closest C++ Python O(n^2) O(1) Medium Two Pointers
018 4 Sum C++ Python O(n^3) O(1) Medium Two Pointers
026 Remove Duplicates from Sorted Array C++ Python O(n) O(1) Easy Two Pointers
027 Remove Element C++ Python O(n) O(1) Easy
031 Next Permutation C++ Python O(n) O(1) Medium Tricky
041 First Missing Positive C++ Python O(n) O(1) Hard Tricky
048 Rotate Image C++ Python O(n^2) O(1) Medium
054 Spiral Matrix C++ Python O(m * n) O(1) Medium
059 Spiral Matrix II C++ Python O(n^2) O(1) Medium
066 Plus One C++ Python O(n) O(1) Easy
073 Set Matrix Zeroes C++ Python O(m * n) O(1) Medium
080 Remove Duplicates from Sorted Array II C++ Python O(n) O(1) Medium Two Pointers
118 Pascal's Triangle C++ Python O(n^2) O(1) Easy
119 Pascal's Triangle II C++ Python O(n^2) O(1) Easy
121 Best Time to Buy and Sell Stock C++ Python O(n) O(1) Easy
128 Longest Consecutive Sequence C++ Python O(n) O(n) Hard Tricky
157 Read N Characters Given Read4 C++ Python O(n) O(1) Easy πŸ“–
158 Read N Characters Given Read4 II - Call multiple times C++ Python O(n) O(1) Hard πŸ“–
163 Missing Ranges C++ Python O(n) O(1) Medium πŸ“–
169 Majority Element C++ Python O(n) O(1) Easy
189 Rotate Array C++ Python O(n) O(1) Easy
209 Minimum Size Subarray Sum C++ Python O(n) O(1) Medium Binary Search
215 Kth Largest Element in an Array C++ Python O(n) ~ O(n^2) O(1) Medium EPI
228 Summary Ranges C++ Python O(n) O(1) Medium
229 Majority Element II C++ Python O(n) O(1) Medium
238 Product of Array Except Self C++ Python O(n) O(1) Medium LintCode
240 Search a 2D Matrix II C++ Python O(m + n) O(1) Medium EPI, LintCode
243 Shortest Word Distance C++ Python O(n) O(1) Easy πŸ“–
245 Shortest Word Distance III C++ Python O(n) O(1) Medium πŸ“–
251 Flatten 2D Vector C++ Python O(1) O(1) Medium πŸ“–
277 Find the Celebrity C++ Python O(n) O(1) Medium πŸ“–, EPI
289 Game of Life C++ Python O(m * n) O(1) Medium
293 Flip Game C++ Python O(n * (c+1)) O(1) Easy πŸ“–
296 Best Meeting Point C++ Python O(m * n) O(m + n) Hard πŸ“–
311 Sparse Matrix Multiplication C++ Python O(m * n * l) O(m * l) Medium πŸ“–
334 Increasing Triplet Subsequence C++ Python O(n) O(1) Medium
370 Range Addition C++ Python O(k + n) O(1) Medium πŸ“–
384 Shuffle an Array C++ Python O(n) O(n) Medium EPI
396 Rotate Function C++ Python O(n) O(1) Easy
412 Fizz Buzz C++ Python O(n) O(1) Easy
414 Third Maximum Number C++ Python O(n) O(1) Easy
419 Battleships in a Board C++ Python O(m * n) O(1) Medium
422 Valid Word Square C++ Python O(m * n) O(1) Easy πŸ“–
442 Find All Duplicates in an Array C++ Python O(n) O(1) Medium
448 Find All Numbers Disappeared in an Array C++ Python O(n) O(1) Easy
531 Lonely Pixel I C++ Python O(m * n) O(m + n) Medium πŸ“–
533 Lonely Pixel II C++ Python O(m * n) O(m * n) Medium πŸ“–
565 Array Nesting C++ Python O(n) O(1) Medium
566 Reshape the Matrix C++ Python O(m * n) O(m * n) Easy
581 Shortest Unsorted Continuous Subarray C++ Python O(n) O(1) Easy
605 Can Place Flowers C++ Python O(n) O(1) Easy
624 Maximum Distance in Arrays C++ Python O(n) O(1) Easy πŸ“–
643 Maximum Average Subarray I C++ Python O(n) O(1) Easy Math
644 Maximum Average Subarray II C++ Python O(n) O(n) Hard πŸ“– Math
661 Image Smoother C++ Python O(m * n) O(1) Easy
665 Non-decreasing Array C++ Python O(n) O(1) Easy
667 Beautiful Arrangement II C++ Python O(n) O(1) Medium
670 Maximum Swap C++ Python O(logn) O(logn) Medium
674 Longest Continuous Increasing Subsequence C++ Python O(n) O(1) Easy
683 K Empty Slots C++ Python O(n) O(n) Hard
697 Degree of an Array C++ Python O(n) O(n) Easy
713 Subarray Product Less Than K C++ Python O(n) O(1) Medium
717 1-bit and 2-bit Characters C++ Python O(n) O(1) Easy Greedy
723 Candy Crush C++ Python O((R * C)^2) O(1) Medium
724 Find Pivot Index C++ Python O(n) O(1) Easy
729 My Calendar I C++ Python O(nlogn) O(n) Medium
731 My Calendar II C++ Python O(n^2) O(n) Medium
732 My Calendar III C++ Python O(n^2) O(n) Hard
747 Largest Number At Least Twice of Others C++ Python O(n) O(1) Easy
755 Pour Water C++ Python O(v * n) O(1) Medium
766 Toeplitz Matrix C++ Python O(m * n) O(1) Easy
768 Max Chunks To Make Sorted II C++ Python O(nlogn) O(n) Hard
769 Max Chunks To Make Sorted C++ Python O(n) O(1) Medium
778 Swim in Rising Water C++ Python O(n^2) O(n^2) Hard Union Find
792 Number of Matching Subsequences C++ Python O(n + w) O(1) Medium
794 Valid Tic-Tac-Toe State C++ Python O(1) O(1) Medium
795 Number of Subarrays with Bounded Maximum C++ Python O(n) O(1) Medium
803 Bricks Falling When Hit C++ Python O(r * c) O(r * c) Hard Union Find
807 Max Increase to Keep City Skyline C++ Python O(n^2) O(n) Medium
821 Shortest Distance to a Character C++ Python O(n) O(1) Easy
830 Positions of Large Groups C++ Python O(n) O(1) Easy
832 Flipping an Image C++ Python O(n^2) O(1) Easy
835 Image Overlap C++ Python O(n^4) O(n^2) Medium
840 Magic Squares In Grid C++ Python O(m * n) O(1) Easy
842 Split Array into Fibonacci Sequence C++ Python O(n^3) O(n) Medium
845 Longest Mountain in Array C++ Python O(n) O(1) Medium
849 Maximize Distance to Closest Person C++ Python O(n) O(1) Easy
860 Lemonade Change C++ Python O(n) O(1) Easy
868 Transpose Matrix C++ Python O(r * c) O(1) Easy
885 Spiral Matrix III C++ Python O(max(m, n)^2) O(1) Medium
888 Fair Candy Swap C++ Python O(m + n) O(m + n) Easy
896 Monotonic Array C++ Python O(n) O(1) Easy
905 Sort Array By Parity C++ Python O(n) O(1) Easy
909 Snakes and Ladders C++ Python O(n^2) O(n^2) Medium
915 Partition Array into Disjoint Intervals C++ Python O(n) O(n) Medium
918 Maximum Sum Circular Subarray C++ Python O(n) O(1) Medium

String

# Title Solution Time Space Difficulty Tag Note
005 Longest Palindromic Substring C++ Python O(n) O(n) Medium Manacher's Algorithm
006 ZigZag Conversion C++ Python O(n) O(1) Easy
008 String to Integer (atoi) C++ Python O(n) O(1) Easy
014 Longest Common Prefix C++ Python O(n * k) O(1) Easy
028 Implement strStr() C++ Python O(n + k) O(k) Easy KMP Algorithm
038 Count and Say C++ Python O(n * 2^n) O(2^n) Easy
043 Multiply Strings C++ Python O(m * n) O(m + n) Medium
058 Length of Last Word C++ Python O(n) O(1) Easy
067 Add Binary C++ Python O(n) O(1) Easy
068 Text Justification C++ Python O(n) O(1) Hard
125 Valid Palindrome C++ Python O(n) O(1) Easy
151 Reverse Words in a String C++ Python O(n) O(1) Medium
161 One Edit Distance C++ Python O(m + n) O(1) Medium πŸ“–
165 Compare Version Numbers C++ Python O(n) O(1) Easy
186 Reverse Words in a String II C++ Python O(n) O(1) Medium πŸ“–
214 Shortest Palindrome C++ Python O(n) O(n) Hard KMP Algorithm Manacher's Algorithm
242 Valid Anagram C++ Python O(n) O(1) Easy LintCode
271 Encode and Decode Strings C++ Python O(n) O(1) Medium πŸ“–
273 Integer to English Words C++ Python O(1) O(1) Hard
306 Addictive Number C++ Python O(n^3) O(n) Medium
383 Ransom Note C++ Python O(n) O(1) Easy EPI
405 Convert a Number to Hexadecimal C++ Python O(n) O(1) Easy
408 Valid Word Abbreviation C++ Python O(n) O(1) Easy πŸ“–
415 Add Strings C++ Python O(n) O(1) Easy
420 Strong Password Checker C++ Python O(n) O(1) Hard
434 Number of Segments in a String C++ Python O(n) O(1) Easy
443 String Compression C++ Python O(n) O(1) Easy
459 Repeated Substring Pattern C++ Python O(n) O(n) Easy KMP Algorithm
468 Validate IP Address C++ Python O(1) O(1) Medium
520 Detect Capital C++ Python O(l) O(1) Easy
521 Longest Uncommon Subsequence I C++ Python O(min(a, b)) O(1) Easy
522 Longest Uncommon Subsequence II C++ Python O(l * n^2) O(1) Medium Sort
524 Longest Word in Dictionary through Deleting C++ Python O((d * l) * logd) O(1) Medium Sort
527 Word Abbreviation C++ Python O(n * l) ~ O(n^2 * l^2) O(n * l) Hard πŸ“–
539 Minimum Time Difference C++ Python O(nlogn) O(n) Medium
541 Reverse String II C++ Python O(n) O(1) Easy
551 Student Attendance Record I C++ Python O(n) O(1) Easy
556 Next Greater Element III C++ Python O(1) O(1) Medium
557 Reverse Words in a String III C++ Python O(n) O(1) Easy
564 Find the Closest Palindrome C++ Python O(l) O(l) Hard
591 Tag Validator C++ Python O(n) O(n) Hard
616 Add Bold Tag in String C++ Python O(n * d * l) O(n) Medium πŸ“–
647 Palindromic Substrings C++ Python O(n) O(n) Medium Manacher's Algorithm
648 Replace Words C++ Python O(n) O(t) Medium Trie
657 Judge Route Circle C++ Python O(n) O(1) Easy
678 Valid Parenthesis String C++ Python O(n) O(1) Medium
680 Valid Palindrome II C++ Python O(n) O(1) Easy
681 Next Closest Time C++ Python O(1) O(1) Medium
686 Repeated String Match C++ Python O(n + m) O(1) Easy Rabin-Karp Algorithm
696 Count Binary Substrings C++ Python O(n) O(1) Easy
720 Longest Word in Dictionary C++ Python O(n) O(t) Easy Trie
722 Remove Comments C++ Python O(n) O(k) Medium
751 IP to CIDR C++ Python O(n) O(1) Medium
758 Bold Words in String C++ Python O(n * l) O(t) Easy πŸ“–, variant of Add Bold Tag in String
791 Custom Sort String C++ Python O(n) O(1) Medium
796 Rotate String C++ Python O(n) O(1) Easy KMP Algorithm Rabin-Karp Algorithm
804 Unique Morse Code Words C++ Python O(n) O(n) Easy
806 Number of Lines To Write String C++ Python O(n) O(1) Easy
809 Expressive Words C++ Python O(n + s) O(l + s) Medium
816 Ambiguous Coordinates C++ Python O(n^4) O(n) Medium
819 Most Common Word C++ Python O(m + n) O(m + n) Easy
820 Short Encoding of Words C++ Python O(n) O(t) Medium Trie
824 Goat Latin C++ Python O(n + w^2) O(l) Easy
831 Masking Personal Information C++ Python O(1) O(1) Medium
833 Find And Replace in String C++ Python O(n + m) O(n) Medium
839 Similar String Groups C++ Python O(n^2 * l) O(n) Hard Union Find
848 Shifting Letters C++ Python O(n) O(1) Medium
859 Buddy Strings C++ Python O(n) O(1) Easy
880 Decoded String at Index C++ Python O(n) O(1) Medium
884 Uncommon Words from Two Sentences C++ Python O(m + n) O(m + n) Easy
890 Find and Replace Pattern C++ Python O(n * l) O(1) Medium
893 Groups of Special-Equivalent Strings C++ Python O(n * l) O(n) Easy
916 Word Subsets C++ Python O(m + n) O(1) Medium
917 Reverse Only Letters C++ Python O(n) O(1) Easy

Linked List

# Title Solution Time Space Difficulty Tag Note
002 Add Two Numbers C++ Python O(n) O(1) Medium
021 Merge Two Sorted Lists C++ Python O(n) O(1) Easy
023 Merge k Sorted Lists C++ Python O(nlogk) O(1) Hard Heap, Divide and Conquer
024 Swap Nodes in Pairs C++ Python O(n) O(1) Easy
025 Reverse Nodes in k-Group C++ Python O(n) O(1) Hard
061 Rotate List C++ Python O(n) O(1) Medium
082 Remove Duplicates from Sorted List II C++ Python O(n) O(1) Medium
083 Remove Duplicates from Sorted List C++ Python O(n) O(1) Easy
092 Reverse Linked List II C++ Python O(n) O(1) Medium
138 Copy List with Random Pointer C++ Python O(n) O(1) Hard
160 Intersection of Two Linked Lists C++ Python O(m + n) O(1) Easy
203 Remove Linked List Elements C++ Python O(n) O(1) Easy
206 Reverse Linked List C++ Python O(n) O(1) Easy
234 Palindrome Linked List C++ Python O(n) O(1) Easy
237 Delete Node in a Linked List C++ Python O(1) O(1) Easy LintCode
328 Odd Even Linked List C++ Python O(n) O(1) Medium
369 Plus One Linked List C++ Python O(n) O(1) Medium πŸ“– Two Pointers
445 Add Two Numbers II C++ Python O(m + n) O(m + n) Medium
725 Split Linked List in Parts C++ Python O(n + k) O(1) Medium
817 Linked List Components C++ Python O(m + n) O(m) Medium

Stack

# Title Solution Time Space Difficulty Tag Note
020 Valid Parentheses C++ Python O(n) O(n) Easy
032 Longest Valid Parentheses C++ Python O(n) O(1) Hard
071 Simplify Path C++ Python O(n) O(n) Medium
084 Largest Rectangle in Histogram C++ Python O(n) O(n) Hard Ascending Stack, DP
085 Maximal Rectangle C++ Python O(m * n) O(n) Hard EPI Ascending Stack
101 Symmetric Tree C++ Python O(n) O(h) Easy
150 Evaluate Reverse Polish Notation C++ Python O(n) O(n) Medium
155 Min Stack C++ Python O(n) O(1) Easy
173 Binary Search Tree Iterator C++ Python O(1) O(h) Medium
224 Basic Calculator C++ Python O(n) O(n) Hard
227 Basic Calculator II C++ Python O(n) O(n) Medium
232 Implement Queue using Stacks C++ Python O(1), amortized O(n) Easy EPI, LintCode
255 Verify Preorder Sequence in Binary Search Tree C++ Python O(n) O(1) Medium πŸ“–
272 Closest Binary Search Tree Value II C++ Python O(h + k) O(h) Hard πŸ“–
331 Verify Preorder Serialization of a Binary Tree C++ Python O(n) O(1) Medium
341 Flatten Nested List Iterator C++ Python O(n) O(h) Medium πŸ“– Iterator
385 Mini Parser C++ Python O(n) O(h) Medium
394 Decode String C++ Python O(n) O(h) Medium
439 Ternary Expression Parser C++ Python O(n) O(1) Medium πŸ“–
456 132 Pattern C++ Python O(n) O(n) Medium
636 Exclusive Time of Functions C++ Python O(n) O(n) Medium
682 Baseball Game C++ Python O(n) O(n) Easy
726 Number of Atoms C++ Python O(n) O(n) Hard
735 Asteroid Collision C++ Python O(n) O(n) Medium
736 Parse Lisp Expression C++ Python O(n^2) O(n^2) Hard
739 Daily Temperatures C++ Python O(n) O(n) Medium
770 Basic Calculator IV C++ Python add: O(d * t)
sub: O(d * t)
mul: O(d * t^2)
eval: O(d * t)
to_list: O(d * tlogt)
O(e + d * t) Hard
772 Basic Calculator III C++ Python O(n) O(n) Hard
853 Car Fleet C++ Python O(nlogn) O(n) Medium
856 Score of Parentheses C++ Python O(n) O(1) Medium
872 Leaf-Similar Trees C++ Python O(n) O(h) Easy
895 Maximum Frequency Stack C++ Python O(1) O(n) Hard Hash
901 Online Stock Span C++ Python O(n) O(n) Medium

Queue

# Title Solution Time Space Difficulty Tag Note
239 Sliding Window Maximum C++ Python O(n) O(k) Hard EPI, LintCode
281 Zigzag Iterator C++ Python O(n) O(k) Medium πŸ“–
346 Moving Average from Data Stream C++ Python O(1) O(w) Easy πŸ“–
862 Shortest Subarray with Sum at Least K C++ Python O(n) O(n) Hard

Heap

# Title Solution Time Space Difficulty Tag Note
264 Ugly Number II C++ Python O(n) O(1) Medium CTCI, LintCode BST, Heap
295 Find Median from Data Stream C++ Python O(nlogn) O(n) Hard EPI, LintCode BST, Heap
313 Super Ugly Number C++ Python O(n * k) O(n + k) Medium BST, Heap
358 Rearrange String k Distance Apart C++ Python O(n) O(n) Hard πŸ“– Greedy, Heap
373 Find K Pairs with Smallest Sums C++ Python O(k * log(min(n, m, k))) O(min(n, m, k)) Medium
378 Kth Smallest Element in a Sorted Matrix C++ Python O(k * log(min(n, m, k))) O(min(n, m, k)) Medium LintCode
407 Trapping Rain Water II C++ Python O(m * n * (logm + logn)) O(m * n) Hard LintCode
632 Smallest Range C++ Python O(nlogk) O(k) Hard
846 Hand of Straights C++ Python O(nlogn) O(n) Medium
855 Exam Room C++ Python seat: O(logn)
leave: O(logn)
O(n) Medium BST, Hash
857 Minimum Cost to Hire K Workers C++ Python O(nlogn) O(n) Hard Sort
871 Minimum Number of Refueling Stops C++ Python O(nlogn) O(n) Hard Sort

Tree

# Title Solution Time Space Difficulty Tag Note
094 Binary Tree Inorder Traversal C++ Python O(n) O(1) Medium Morris Traversal
099 Recover Binary Search Tree C++ Python O(n) O(1) Hard Morris Traversal
144 Binary Tree Preorder Traversal C++ Python O(n) O(1) Medium Morris Traversal
145 Binary Tree Postorder Traversal C++ Python O(n) O(1) Hard Morris Traversal
208 Implement Trie (Prefix Tree) C++ Python O(n) O(1) Medium Trie
211 Add and Search Word - Data structure design C++ Python O(min(n, h)) O(min(n, h)) Medium Trie, DFS
226 Invert Binary Tree C++ Python O(n) O(h), O(w) Easy
297 Serialize and Deserialize Binary Tree C++ Python O(n) O(h) Hard LintCode DFS
307 Range Sum Query - Mutable C++ Python ctor: O(n), update: O(logn), query: O(logn) O(n) Medium LintCode DFS, Segment Tree, BIT
308 Range Sum Query 2D - Mutable C++ Python ctor: O(m * n), update: O(logm + logn), query: O(logm + logn) O(m * n) Hard πŸ“– DFS, Segment Tree, BIT
315 Count of Smaller Numbers After Self C++ Python O(nlogn) O(n) Hard LintCode BST, BIT, Divide and Conquer
525 Contiguous Array C++ Python O(n) O(n) Medium
529 Minesweeper C++ Python O(m * n) O(m + n) Medium
536 Construct Binary Tree from String C++ Python O(n) O(h) Medium πŸ“–
538 Convert BST to Greater Tree C++ Python O(n) O(h) Easy
543 Diameter of Binary Tree C++ Python O(n) O(h) Easy
545 Boundary of Binary Tree C++ Python O(n) O(h) Medium πŸ“–
548 Split Array with Equal Sum C++ Python O(n^2) O(n) Medium πŸ“–
563 Binary Tree Tilt C++ Python O(n) O(n) Easy
572 Subtree of Another Tree C++ Python O(m * n) O(h) Easy
606 Construct String from Binary Tree C++ Python O(n) O(h) Easy
617 Merge Two Binary Trees C++ Python O(n) O(h) Easy
623 Add One Row to Tree C++ Python O(n) O(h) Medium
637 Average of Levels in Binary Tree C++ Python O(n) O(h) Easy
652 Find Duplicate Subtrees C++ Python O(n) O(n) Medium DFS, Hash
653 Two Sum IV - Input is a BST C++ Python O(n) O(h) Easy Two Pointers
654 Maximum Binary Tree C++ Python O(n) O(n) Medium LintCode Descending Stack
655 Print Binary Tree C++ Python O(n) O(h) Medium
662 Maximum Width of Binary Tree C++ Python O(n) O(h) Medium DFS
663 Equal Tree Partition C++ Python O(n) O(n) Medium πŸ“– Hash
677 Map Sum Pairs C++ Python O(n) O(t) Medium Trie
684 Redundant Connection C++ Python O(n) O(n) Medium Union Find
685 Redundant Connection II C++ Python O(n) O(n) Hard Union Find
687 Longest Univalue Path C++ Python O(n) O(h) Easy
699 Falling Squares C++ Python O(nlogn) O(n) Hard Segment Tree
814 Binary Tree Pruning C++ Python O(n) O(h) Medium DFS
850 Rectangle Area II C++ Python O(nlogn) O(n) Hard Segment Tree
863 All Nodes Distance K in Binary Tree C++ Python O(n) O(n) Medium DFS + BFS
866 Smallest Subtree with all the Deepest Nodes C++ Python O(n) O(h) Medium DFS
889 Construct Binary Tree from Preorder and Postorder Traversal C++ Python O(n) O(h) Medium DFS, stack
897 Increasing Order Search Tree C++ Python O(n) O(h) Easy DFS
919 Complete Binary Tree Inserter C++ Python ctor: O(n)
insert: O(1)
get_root: O(1)
O(n) Medium

Hash Table

# Title Solution Time Space Difficulty Tag Note
001 Two Sum C++ Python O(n) O(n) Easy
003 Longest Substring Without Repeating Characters C++ Python O(n) O(1) Medium
030 Substring with Concatenation of All Words C++ Python O((m + n) * k) O(n * k) Hard
036 Valid Sudoku C++ Python O(9^2) O(9) Easy
049 Group Anagrams C++ Python O(n * glogg) O(n) Medium
076 Minimum Window Substring C++ Python O(n) O(k) Hard
149 Max Points on a Line C++ Python O(n^2) O(n) Hard
159 Longest Substring with At Most Two Distinct Characters C++ Python O(n) O(1) Hard πŸ“–
170 Two Sum III - Data structure design C++ Python O(n) O(n) Easy πŸ“–
187 Repeated DNA Sequences Python O(n) O(n) Medium
202 Happy Number C++ Python O(k) O(k) Easy
204 Count Primes C++ Python O(n) O(n) Easy
205 Isomorphic Strings C++ Python O(n) O(1) Easy
217 Contains Duplicate C++ Python O(n) O(n) Easy
219 Contains Duplicate II C++ Python O(n) O(n) Easy
244 Shortest Word Distance II C++ Python ctor: O(n), lookup: O(a + b) O(n) Medium πŸ“–
246 Strobogrammatic Number C++ Python O(n) O(1) Easy πŸ“–
249 Group Shifted Strings C++ Python O(nlogn) O(n) Easy πŸ“–
266 Palindrome Permutation C++ Python O(n) O(1) Easy πŸ“–
288 Unique Word Abbreviation C++ Python ctor: O(n), lookup: O(1) O(k) Easy πŸ“–
290 Word Pattern C++ Python O(n) O(c) Easy variant of Isomorphic Strings
299 Bulls and Cows C++ Python O(n) O(1) Easy
305 Number of Islands II C++ Python O(k) O(k) Hard LintCode, πŸ“– Union Find
314 Binary Tree Vertical Order Traversal C++ Python O(n) O(n) Medium πŸ“– BFS
323 Number of Connected Components in an Undirected Graph C++ Python O(n) O(n) Medium πŸ“– Union Find
325 Maximum Size Subarray Sum Equals k C++ Python O(n) O(n) Medium πŸ“–
336 Palindrome Pairs C++ Python O(n * k^2) O(n * k) Hard
340 Longest Substring with At Most K Distinct Characters C++ Python O(n) O(1) Hard πŸ“–
356 Line Reflection C++ Python O(n) O(n) Medium πŸ“– Hash, Two Pointers
387 First Unique Character in a String C++ Python O(n) O(n) Easy
388 Longest Absolute File Path C++ Python O(n) O(d) Medium Stack
409 Longest Palindrome C++ Python O(n) O(1) Easy
424 Longest Repeating Character Replacement C++ Python O(n) O(1) Medium
438 Find All Anagrams in a String C++ Python O(n) O(1) Easy
447 Number of Boomerangs C++ Python O(n^2) O(n) Easy
454 4Sum II C++ Python O(n^2) O(n^2) Medium
470 Implement Rand10() Using Rand7() C++ Python O(1) O(1) Medium
473 Matchsticks to Square C++ Python O(n * s * 2^n) O(n * (2^n + s)) Medium
523 Continuous Subarray Sum C++ Python O(n) O(k) Medium
532 K-diff Pairs in an Array C++ Python O(n) O(n) Easy
554 Brick Wall C++ Python O(n) O(m) Medium
560 Subarray Sum Equals K C++ Python O(n) O(n) Medium
561 Array Partition I C++ Python O(r) O(r) Easy
575 Distribute Candies C++ Python O(n) O(n) Easy
594 Longest Harmonious Subsequence C++ Python O(n) O(n) Easy
599 Minimum Index Sum of Two Lists C++ Python O((m + n) * l) O(m * l) Easy
609 Find Duplicate File in System C++ Python O(n * l) O(n * l) Medium
721 Accounts Merge C++ Python O(nlogn) O(n) Medium Union Find
734 Sentence Similarity C++ Python O(n + p) O(p) Easy
737 Sentence Similarity II C++ Python O(n + p) O(p) Medium Union Find
748 Shortest Completing Word C++ Python O(n) O(1) Easy
760 Find Anagram Mappings C++ Python O(n) O(n) Easy
771 Jewels and Stones C++ Python O(m + n) O(n) Easy
811 Subdomain Visit Count C++ Python O(n) O(n) Easy
822 Card Flipping Game C++ Python O(n) O(n) Medium
825 Friends Of Appropriate Ages C++ Python O(a^2 + n) O(a) Medium
869 Reordered Power of 2 C++ Python O(1) O(1) Medium
873 Length of Longest Fibonacci Subsequence C++ Python O(n^2) O(n) Medium

Math

# Title Solution Time Space Difficulty Tag Note
007 Reverse Integer C++ Python O(1) O(1) Easy
009 Palindrome Number C++ Python O(1) O(1) Easy
012 Integer to Roman C++ Python O(n) O(1) Medium
013 Roman to Integer C++ Python O(n) O(1) Easy
029 Divide Two Integers C++ Python O(1) O(1) Medium
050 Pow(x, n) C++ Python O(1) O(1) Medium
060 Permutation Sequence C++ Python O(n^2) O(n) Medium Cantor Ordering
065 Valid Number C++ Python O(n) O(1) Hard Automata
089 Gray Code C++ Python O(2^n) O(1) Medium
166 Fraction to Recurring Decimal C++ Python O(logn) O(1) Medium
168 Excel Sheet Column Title C++ Python O(logn) O(1) Easy
171 Excel Sheet Column Number C++ Python O(n) O(1) Easy
172 Factorial Trailing Zeroes C++ Python O(1) O(1) Easy
223 Rectangle Area C++ Python O(1) O(1) Easy
233 Number of Digit One C++ Python O(1) O(1) Hard CTCI, LintCode
248 Strobogrammatic Number III C++ Python O(5^(n/2)) O(n) Hard πŸ“–
258 Add Digits C++ Python O(1) O(1) Easy
263 Ugly Number C++ Python O(1) O(1) Easy
292 Nim Game C++ Python O(1) O(1) Easy LintCode
319 Bulb Switcher C++ Python O(1) O(1) Medium
326 Power of Three C++ Python O(1) O(1) Easy
335 Self Crossing C++ Python O(n) O(1) Hard
338 Counting Bits C++ Python O(n) O(n) Medium
343 Integer Break C++ Python O(logn) O(1) Medium Tricky, DP
365 Water and Jug Problem C++ Python O(logn) O(1) Medium BΓ©zout's identity
372 Super Pow C++ Python O(n) O(1) Medium
382 Linked List Random Node C++ Python O(n) O(1) Medium Reservoir Sampling
386 Lexicographical Numbers C++ Python O(n) O(1) Medium
390 Elimination Game C++ Python O(logn) O(1) Medium
391 Perfect Rectangle C++ Python O(n) O(n) Hard
398 Random Pick Index C++ Python O(n) O(1) Medium Reservoir Sampling
400 Nth Digit C++ Python O(logn) O(1) Easy
413 Arithmetic Slices C++ Python O(n) O(1) Medium
423 Reconstruct Original Digits from English C++ Python O(n) O(1) Medium GCJ2016 - Round 1B
441 Arranging Coins C++ Python O(nlogn) O(1) Easy Binary Search
453 Minimum Moves to Equal Array Elements C++ Python O(n) O(1) Easy
458 Poor Pigs C++ Python O(n) O(1) Easy
469 Convex Polygon C++ Python O(n) O(1) Medium πŸ“–
470 Implement Rand10() Using Rand7() C++ Python O(1) O(1) Medium
478 Generate Random Point in a Circle C++ Python O(1) O(1) Medium
497 Random Point in Non-overlapping Rectangles C++ Python ctor: O(n)
pick: O(logn)
O(n) Medium
517 Super Washing Machines C++ Python O(n) O(1) Hard
519 Random Flip Matrix C++ Python ctor: O(1)
pick: O(1) reset: O(n)
O(n) Medium
528 Random Pick with Weight C++ Python ctor: O(n)
pick: O(logn)
O(n) Medium
537 Complex Number Multiplication C++ Python O(1) O(1) Medium
553 Optimal Division C++ Python O(n) O(1) Medium
573 Squirrel Simulation C++ Python O(n) O(1) Medium πŸ“–
592 Fraction Addition and Subtraction C++ Python O(nlogx) O(n) Medium
593 Valid Square C++ Python O(1) O(1) Medium
598 Range Addition II C++ Python O(p) O(1) Easy
625 Minimum Factorization C++ Python O(loga) O(1) Medium πŸ“–
628 Maximum Product of Three Numbers C++ Python O(n) O(1) Easy
633 Sum of Square Numbers C++ Python O(sqrt(c) * logc) O(1) Easy
634 Find the Derangement of An Array C++ Python O(n) O(1) Medium πŸ“–
640 Solve the Equation C++ Python O(n) O(n) Medium
651 4 Keys Keyboard C++ Python O(1) O(1) Medium πŸ“– Math, DP
660 Remove 9 C++ Python O(logn) O(1) Hard πŸ“–
672 Bulb Switcher II C++ Python O(1) O(1) Medium
728 Self Dividing Numbers C++ Python O(n) O(1) Medium
754 Reach a Number C++ Python O(logn) O(1) Medium
775 Global and Local Inversions C++ Python O(n) O(1) Medium
779 K-th Symbol in Grammar C++ Python O(1) O(1) Medium
780 Reaching Points C++ Python O(log(max(m, n))) O(1) Hard
781 Rabbits in Forest C++ Python O(n) O(n) Medium
782 Transform to Chessboard C++ Python O(n^2) O(n) Hard
789 Escape The Ghosts C++ Python O(n) O(1) Medium
800 Similar RGB Color C++ Python O(1) O(1) Easy πŸ“–
810 Chalkboard XOR Game C++ Python O(1) O(1) Hard
812 Largest Triangle Area C++ Python O(n^3) O(1) Easy
829 Consecutive Numbers Sum C++ Python O(sqrt(n)) O(1) Medium
836 Rectangle Overlap C++ Python O(1) O(1) Easy
858 Mirror Reflection C++ Python O(1) O(1) Medium
867 Prime Palindrome C++ Python O(n^(1/2) * (logn + n^(1/2))) O(logn) Medium
883 Projection Area of 3D Shapes C++ Python O(n^2) O(1) Easy
887 Super Egg Drop C++ Python O(klogn) O(1) Hard
891 Sum of Subsequence Widths C++ Python O(n) O(1) Hard
899 Orderly Queue C++ Python O(n^2) O(n) Hard
902 Numbers At Most N Given Digit Set C++ Python O(logn) O(logn) Hard
906 Super Palindromes C++ Python O(n^0.25 * logn) O(logn) Hard
907 Sum of Subarray Minimums C++ Python O(n) O(n) Medium Ascending Stack
908 Smallest Range I C++ Python O(n) O(1) Easy
910 Smallest Range II C++ Python O(nlogn) O(1) Medium
914 X of a Kind in a Deck of Cards C++ Python O(n * (logn)^2) O(n) Easy

Sort

# Title Solution Time Space Difficulty Tag Note
056 Merge Intervals C++ Python O(nlogn) O(1) Hard
057 Insert Interval C++ Python O(n) O(1) Hard
075 Sort Colors C++ Python O(n) O(1) Medium Tri Partition
088 Merge Sorted Array C++ Python O(n) O(1) Easy
147 Insertion Sort List C++ Python O(n^2) O(1) Medium
148 Sort List C++ Python O(nlogn) O(logn) Medium
164 Maximum Gap C++ Python O(n) O(n) Hard Tricky
179 Largest Number C++ Python O(nlogn) O(1) Medium
218 The Skyline Problem C++ Python O(nlogn) O(n) Hard Sort, BST
252 Meeting Rooms C++ Python O(nlogn) O(n) Easy πŸ“–
253 Meeting Rooms II C++ Python O(nlogn) O(n) Medium πŸ“–
274 H-Index C++ Python O(n) O(n) Medium Counting Sort
280 Wiggle Sort C++ Python O(n) O(1) Medium πŸ“–
324 Wiggle Sort II C++ Python O(n) on average O(1) Medium variant of Sort Colors Tri Partition
347 Top K Frequent Elements C++ Python O(n) O(n) Medium Quick Select, Heap, Bucket Sort
406 Queue Reconstruction by Height C++ Python O(n * sqrt(n)) O(n) Medium Tricky
451 Sort Characters By Frequency C++ Python O(n) O(n) Medium
692 Top K Frequent Words C++ Python O(n + klogk) on average O(n) Medium Quick Select, Heap, Bucket Sort

Two Pointers

# Title Solution Time Space Difficulty Tag Note
019 Remove Nth Node From End of List C++ Python O(n) O(1) Easy
086 Partition List C++ Python O(n) O(1) Medium
141 Linked List Cycle C++ Python O(n) O(1) Easy
142 Linked List Cycle II C++ Python O(n) O(1) Medium
143 Reorder List C++ Python O(n) O(1) Medium
167 Two Sum II - Input array is sorted C++ Python O(n) O(1) Medium
259 3Sum Smaller C++ Python O(n^2) O(1) Medium πŸ“–, LintCode
283 Move Zeroes C++ Python O(n) O(1) Easy
287 Find the Duplicate Number C++ Python O(n) O(1) Hard Binary Search, Two Pointers
344 Reverse String C++ Python O(n) O(1) Easy
345 Reverse Vowels of a String C++ Python O(n) O(1) Easy
349 Intersection of Two Arrays C++ Python O(m + n) O(min(m, n)) Easy EPI Hash, Binary Search
350 Intersection of Two Arrays II C++ Python O(m + n) O(1) Easy EPI Hash, Binary Search
360 Sort Transformed Array C++ Python O(n) O(1) Medium πŸ“–
457 Circular Array Loop C++ Python O(n) O(1) Medium
567 Permutation in String C++ Python O(n) O(1) Medium
611 Valid Triangle Number C++ Python O(n^2) O(1) Medium
777 Swap Adjacent in LR String C++ Python O(n) O(1) Medium
826 Most Profit Assigning Work C++ Python O(mlogm + nlogn) O(n) Medium
828 Unique Letter String C++ Python O(n) O(1) Hard
844 Backspace String Compare C++ Python O(m + n) O(1) Easy
876 Middle of the Linked List C++ Python O(n) O(1) Easy
904 Fruit Into Baskets C++ Python O(n) O(1) Medium

Recursion

# Title Solution Time Space Difficulty Tag Note
095 Unique Binary Search Trees II C++ Python O(4^n / n^(3/2) O(4^n / n^(3/2) Medium
098 Validate Binary Search Tree C++ Python O(n) O(1) Medium
100 Same Tree C+ Python O(n) O(h) Easy
104 Maximum Depth of Binary Tree C++ Python O(n) O(h) Easy
105 Construct Binary Tree from Preorder and Inorder Traversal C++ Python O(n) O(n) Medium
106 Construct Binary Tree from Inorder and Postorder Traversal C++ Python O(n) O(n) Medium
108 Convert Sorted Array to Binary Search Tree C++ Python O(n) O(logn) Medium
109 Convert Sorted List to Binary Search Tree C++ Python O(n) O(logn) Medium
110 Balanced Binary Tree Python O(n) O(h) Easy
111 Minimum Depth of Binary Tree Python O(n) O(h) Easy
114 Flatten Binary Tree to Linked List Python O(n) O(h) Medium
116 Populating Next Right Pointers in Each Node Python O(n) O(1) Medium
124 Binary Tree Maximum Path Sum Python O(n) O(h) Hard
129 Sum Root to Leaf Numbers Python O(n) O(h) Medium
156 Binary Tree Upside Down Python O(n) O(1) Medium πŸ“–
241 Different Ways to Add Parentheses C++ Python O(n * 4^n / n^(3/2)) O(n * 4^n / n^(3/2)) Medium
298 Binary Tree Longest Consecutive Sequence C++ Python O(n) O(h) Medium πŸ“–
327 Count of Range Sum C++ Python O(nlogn) O(n) Hard
333 Largest BST Subtree C++ Python O(n) O(h) Medium πŸ“–
337 House Robber III C++ Python O(n) O(h) Medium
395 Longest Substring with At Least K Repeating Characters C++ Python O(n) O(1) Medium
404 Sum of Left Leaves C++ Python O(n) O(h) Easy
437 Path Sum III C++ Python O(n) O(h) Easy
544 Output Contest Matches C++ Python O(n) O(n) Medium
549 Binary Tree Longest Consecutive Sequence II C++ Python O(n) O(h) Medium πŸ“–
669 Trim a Binary Search Tree C++ Python O(n) O(h) Easy
671 Second Minimum Node In a Binary Tree C++ Python O(n) O(h) Easy
761 Special Binary String C++ Python O(n^2) O(n) Hard

Binary Search

# Title Solution Time Space Difficulty Tag Note
004 Median of Two Sorted Arrays C++ Python O(log(min(m, n))) O(1) Hard
033 Search in Rotated Sorted Array C++ Python O(logn) O(1) Hard
034 Search for a Range C++ Python O(logn) O(1) Medium
305 Search Insert Position C++ Python O(logn) O(1) Medium
069 Sqrt(x) C++ Python O(logn) O(1) Medium
074 Search a 2D Matrix C++ Python O(logm + logn) O(1) Medium
081 Search in Rotated Sorted Array II C++ Python O(logn) O(1) Medium
153 Find Minimum in Rotated Sorted Array C++ Python O(logn) O(1) Medium
154 Find Minimum in Rotated Sorted Array II C++ Python O(logn) ~ O(n) O(1) Hard
162 Find Peak Element C++ Python O(logn) O(1) Medium
222 Count Complete Tree Nodes C++ Python O((logn)^2) O(1) Medium
275 H-Index II C++ Python O(logn) O(1) Medium Binary Search
278 First Bad Version C++ Python O(logn) O(1) Easy LintCode
300 Longest Increasing Subsequence C++ Python O(nlogn) O(n) Medium CTCI, LintCode Binary Search, DP
302 Smallest Rectangle Enclosing Black Pixels C++ Python O(nlogn) O(1) Hard πŸ“–
354 Russian Doll Envelopes C++ Python O(nlogn) O(1) Hard
363 Max Sum of Rectangle No Larger Than K C++ Python O(min(m, n)^2 * max(m, n) * logn(max(m, n))) O(max(m, n)) Hard
367 Valid Perfect Square C++ Python O(logn) O(1) Medium
374 Guess Number Higher or Lower C++ Python O(logn) O(1) Easy
410 Split Array Largest Sum C++ Python O(nlogs) O(1) Hard
436 Find Right Interval C++ Python O(nlogn) O(n) Medium
475 Heaters C++ Python O((m + n) * logn) O(1) Easy
540 Single Element in a Sorted Array C++ Python O(logn) O(1) Medium
658 Find K Closest Elements C++ Python O(logn + k) O(1) Medium
668 Kth Smallest Number in Multiplication Table C++ Python O(m * log(m * n)) O(1) Hard
719 Find K-th Smallest Pair Distance C++ Python O(nlogn + nlogw) O(1) Hard
744 Find Smallest Letter Greater Than Target C++ Python O(logn) O(1) Easy
774 Minimize Max Distance to Gas Station C++ Python O(nlogr) O(1) Hard
786 K-th Smallest Prime Fraction C++ Python O(nlogr) O(1) Hard
793 Preimage Size of Factorial Zeroes Function C++ Python O((logn)^2) O(1) Hard
852 Peak Index in a Mountain Array C++ Python O(logn) O(1) Easy
864 Random Pick with Blacklist C++ Python ctor: O(b)
pick: O(1)
O(b) Hard
875 Koko Eating Bananas C++ Python O(nlogr) O(1) Medium
878 Nth Magical Number C++ Python O(logn) O(1) Hard
894 All Possible Full Binary Trees C++ Python O(n * 4^n / n^(3/2)) O(n * 4^n / n^(3/2)) Medium
911 Online Election C++ Python ctor: O(n)
query : O(logn)
O(n) Medium

Binary Search Tree

# Title Solution Time Space Difficulty Tag Note
220 Contains Duplicate III C++ Python O(nlogk) O(k) Medium
230 Kth Smallest Element in a BST C++ Python O(max(h, k)) O(min(h, k)) Medium
235 Lowest Common Ancestor of a Binary Search Tree C++ Python O(h) O(1) Easy EPI
270 Closest Binary Search Tree Value C++ Python O(h) O(1) Easy πŸ“–
285 Inorder Successor in BST C++ Python O(h) O(1) Medium πŸ“–
352 Data Stream as Disjoint Intervals C++ Python O(logn) O(n) Hard
449 Serialize and Deserialize BST C++ Python O(n) O(h) Medium
450 Delete Node in a BST C++ Python O(h) O(h) Medium
530 Minimum Absolute Difference in BST C++ Python O(n) O(h) Easy
776 Split BST C++ Python O(n) O(h) Medium πŸ“–
783 Minimum Distance Between BST Nodes C++ Python O(n) O(h) Easy

Breadth-First Search

# Title Solution Time Space Difficulty Tag Note
102 Binary Tree Level Order Traversal C++ Python O(n) O(n) Easy
107 Binary Tree Level Order Traversal II Python O(n) O(n) Easy
103 Binary Tree Zigzag Level Order Traversal Python O(n) O(n) Medium
117 Populating Next Right Pointers in Each Node II Python O(n) O(1) Hard
127 Word Ladder Python O(n * d) O(d) Medium
130 Surrounded Regions C++ Python O(m * n) O(m + n) Medium
133 Clone Graph Python O(n) O(n) Medium
207 Course Schedule Python O(|V| + |E|) O(|E|) Medium Topological Sort
210 Course Schedule II Python O(|V| + |E|) O(|E|) Medium Topological Sort
261 Graph Valid Tree C++ Python O(|V| + |E|) O(|V| + |E|) Medium πŸ“–
269 Alien Dictionary C++ Python O(n) O(1) Hard πŸ“– Topological Sort, BFS, DFS
286 Walls and Gates C++ Python O(m * n) O(g) Medium πŸ“–
310 Minimum Height Trees C++ Python O(n) O(n) Medium
317 Shortest Distance from All Buildings C++ Python O(k * m * n) O(m * n) Hard πŸ“–
433 Minimum Genetic Mutation C++ Python O(n * b) O(b) Medium
444 Sequence Reconstruction C++ Python O(n * s) O(n) Medium πŸ“– Topological Sort
542 01 Matrix C++ Python O(m * n) O(m * n) Medium DP
666 Path Sum IV C++ Python O(n) O(w) Medium πŸ“– Topological Sort
675 Cut Off Trees for Golf Event C++ Python O(t * m * n) O(m * n) Hard A* Search Algorithm
742 Closest Leaf in a Binary Tree C++ Python O(n) O(n) Medium
743 Network Delay Time C++ Python O(|E| * log|V|) O(|E|) Medium Dijkstra's algorithm
752 Open the Lock C++ Python O(k * n^k + d) O(k * n^k + d) Medium
773 Sliding Puzzle C++ Python O((m * n) * (m * n)!) O((m * n) * (m * n)!) Hard A* Search Algorithm
787 Cheapest Flights Within K Stops C++ Python O(|E| * log|V|) O(|E|) Medium Dijkstra's algorithm
815 Bus Routes C++ Python O(|E| + |V|) O(|E| + |V|) Hard
854 K-Similar Strings C++ Python O(n * n!/(c_a!*...*c_z!)) O(n * n!/(c_a!*...*c_z!)) Hard
865 Shortest Path to Get All Keys C++ Python O(k * r * c + k^3*2^k) O(k*2^k) Hard Dijkstra's algorithm
882 Reachable Nodes In Subdivided Graph C++ Python O(|E| * log|V|) O(|E|) Hard Dijkstra's algorithm
886 Possible Bipartition C++ Python O(|V| + |E|) O(|V| + |E|) Medium

Depth-First Search

# Title Solution Time Space Difficulty Tag Note
112 Path Sum Python O(n) O(h) Easy
113 Path Sum II Python O(n) O(h) Medium
199 Binary Tree Right Side View Python O(n) O(h) Medium
200 Number of Islands Python O(m * n) O(m * n) Medium
236 Lowest Common Ancestor of a Binary Tree C++ Python O(n) O(h) Medium EPI
247 Strobogrammatic Number II C++ Python O(n^2 * 5^(n/2)) O(n) Medium πŸ“–
250 Count Univalue Subtrees C++ Python O(n) O(h) Medium πŸ“–
257 Binary Tree Paths C++ Python O(n * h) O(h) Easy
282 Expression Add Operators C++ Python O(4^n) O(n) Hard
301 Remove Invalid Parentheses C++ Python O(C(n, c)) O(c) Hard
329 Longest Increasing Path in a Matrix C++ Python O(m * n) O(m * n) Hard
332 Reconstruct Itinerary C++ Python O(t! / (n1! * n2! * ... nk!)) O(t) Medium
339 Nested List Weight Sum C++ Python O(n) O(h) Easy πŸ“–
364 Nested List Weight Sum II C++ Python O(n) O(h) Medium πŸ“–
366 Find Leaves of Binary Tree C++ Python O(n) O(h) Medium πŸ“–
399 Evaluate Division C++ Python O(q * |V|!) O(e) Medium
417 Pacific Atlantic Water Flow C++ Python O(m * n) O(m * n) Medium
440 K-th Smallest in Lexicographical Order C++ Python O(logn) O(logn) Hard
464 Can I Win C++ Python O(n!) O(n) Medium
515 Find Largest Value in Each Tree Row C++ Python O(n) O(h) Medium
547 Friend Circles C++ Python O(n^2) O(n) Medium Union Find
582 Kill Process C++ Python O(n) O(n) Medium πŸ“– DFS, BFS
638 Shopping Offers C++ Python O(n * 2^n) O(n) Medium
690 Employee Importance C++ Python O(n) O(h) Easy DFS, BFS
694 Number of Distinct Islands C++ Python O(m * n) O(m * n) Medium πŸ“–
695 Max Area of Island C++ Python O(m * n) O(m * n) Easy
711 Number of Distinct Islands II C++ Python O((m * n) * log(m * n)) O(m * n) Hard πŸ“– Hash
733 Max Area of Island C++ Python O(m * n) O(m * n) Easy
749 Contain Virus C++ Python O((m * n)^(4/3)) O(m * n) Hard Simulation
753 Cracking the Safe C++ Python O(k^n) O(k^n) Hard de Bruijn sequences, Lyndon word
756 Pyramid Transition Matrix C++ Python O(a^b) O(a^b) Medium
785 Is Graph Bipartite? C++ Python O(|V| + |E|) O(|V|) Medium
797 All Paths From Source to Target C++ Python O(p + r * n) O(n) Medium
802 Find Eventual Safe States C++ Python O(|V| + |E|) O(|V|) Medium
827 Making A Large Island C++ Python O(n^2) O(n^2) Hard
834 Sum of Distances in Tree C++ Python O(n) O(n) Hard
841 Keys and Rooms C++ Python O(n!) O(n) Medium
851 Loud and Rich C++ Python O(q + r) O(q + r) Medium
913 Cat and Mouse C++ Python O(n^3) O(n^2) Hard

Backtracking

# Title Solution Time Space Difficulty Tag Note
017 Letter Combinations of a Phone Number Python O(n * 4^n) O(n) Medium
022 Generate Parentheses Python O(4^n / n^(3/2)) O(n) Medium
037 Sudoku Solver Python O((9!)^9) O(1) Hard
039 Combination Sum Python O(k * n^k) O(k) Medium
040 Combination Sum II Python O(k * C(n, k)) O(k) Medium
046 Permutations Python O(n * n!) O(n) Medium
047 Permutations II Python O(n * n!) O(n) Medium
051 N-Queens Python O(n!) O(n) Hard
052 N-Queens-II Python O(n!) O(n) Hard
077 Combinations Python O(O(k * C(n, k))) O(k) Medium
079 Word Search Python O(m * n * l) O(l) Medium
093 Restore IP Addresses Python O(1) O(1) Medium
078 Subsets C++ Python O(n * 2^n) O(1) Medium
090 Subsets II C++ Python O(n * 2^n) O(1) Medium
126 Word Ladder II Python O(n * d) O(d) Hard
131 Palindrome Partitioning Python O(n^2) ~ O(2^n) O(n^2) Medium
140 Word Break II C++ Python O(n * l^2 + n * r) O(n^2) Hard
212 Word Search II C++ Python O(m * n * l) O(l) Hard LintCode Trie, DFS
216 Combination Sum III C++ Python O(k * C(n, k)) O(k) Medium
254 Factor Combinations C++ Python O(nlogn) O(logn) Medium πŸ“–
267 Palindrome Permutation II C++ Python O(n * n!) O(n) Medium πŸ“–
291 Word Pattern II C++ Python O(n * C(n - 1, c - 1)) O(n + c) Hard πŸ“–
294 Flip Game II C++ Python O(n + c^2) O(c) Medium πŸ“– DP, Hash
320 Generalized Abbreviation C++ Python O(n * 2^n) O(n) Medium πŸ“–
425 Word Squares C++ Python O(n^2 * n!) O(n^2) Hard πŸ“–
526 Beautiful Arrangement C++ Python O(n!) O(n) Medium
676 Implement Magic Dictionary C++ Python O(n) O(d) Medium Trie, DFS
679 24 Game C++ Python O(1) O(1) Hard DFS
698 Partition to K Equal Sum Subsets C++ Python O(n * 2^n) O(2^n) Medium DFS, DP, Memoization
718 Maximum Length of Repeated Subarray C++ Python O(m * n) O(min(m, n)) Medium DP, Hash, Binary Search
784 Letter Case Permutation C++ Python O(n * 2^n) O(1) Easy

Dynamic Programming

# Title Solution Time Space Difficulty Tag Note
010 Regular Expression Matching Python O(m * n) O(n) Hard
053 Maximum Subarray Python O(n) O(1) Medium
062 Unique Paths Python O(m * n) O(m + n) Medium
063 Unique Paths II Python O(m * n) O(m + n) Medium
064 Minimum Path Sum Python O(m * n) O(m + n) Medium
070 Climbing Stairs Python O(n) O(1) Easy
072 Edit Distance Python O(m * n) O(m + n) Hard
087 Scramble String Python O(n^4) O(n^3) Hard
091 Decode Ways C++ Python O(n) O(1) Medium
096 Unique Binary Search Trees Python O(n) O(1) Medium Math
097 Interleaving String Python O(m * n) O(m + n) Hard
115 Distinct Subsequences Python O(n^2) O(n) Hard
120 Triangle Python O(m * n) O(n) Medium
123 Best Time to Buy and Sell Stock III Python O(n) O(1) Hard
132 Palindrome Partitioning II Python O(n^2) O(n^2) Hard
139 Word Break C++ Python O(n * l^2) O(n) Medium
152 Maximum Product Subarray Python O(n) O(1) Medium
174 Dungeon Game Python O(m * n) O(m + n) Hard
188 Best Time to Buy and Sell Stock IV Python O(k * n) O(k) Hard
198 House Robber Python O(n) O(1) Easy
213 House Robber II C++ Python O(n) O(1) Medium
221 Maximal Square C++ Python O(n^2) O(n) Medium EPI
256 Paint House C++ Python O(n) O(1) Medium πŸ“–
265 Paint House II C++ Python O(n * k) O(k) Hard πŸ“–
276 Paint Fence C++ Python O(n) O(1) Easy πŸ“–
279 Perfect Squares C++ Python O(n * sqrt(n)) O(n) Medium Hash
303 Range Sum Query - Immutable C++ Python ctor: O(n), lookup: O(1) O(n) Easy
304 Range Sum Query 2D - Immutable C++ Python ctor: O(m * n), lookup: O(1) O(m * n) Medium
309 Best Time to Buy and Sell Stock with Cooldown C++ Python O(n) O(1) Medium
312 Burst Balloons C++ Python O(n^3) O(n^2) Hard
322 Coin Change C++ Python O(n * k) O(k) Medium
351 Android Unlock Patterns C++ Python O(9^2 * 2^9) O(9 * 2^9) Medium πŸ“– Backtracking
357 Count Numbers with Unique Digits C++ Python O(n) O(1) Medium Backtracking, Math
361 Bomb Enemy C++ Python O(m * n) O(m * n) Medium πŸ“–
368 Largest Divisible Subset C++ Python O(n^2) O(n) Medium
375 Guess Number Higher or Lower II C++ Python O(n^2) O(n^2) Medium
377 Combination Sum IV C++ Python O(nlogn + n * t) O(t) Medium
403 Frog Jump C++ Python O(n) O(n) ~ O(n^2) Hard
416 Partition Equal Subset Sum C++ Python O(n * s) O(s) Medium
418 Sentence Screen Fitting C++ Python O(r + n * c) O(n) Medium πŸ“–
446 Arithmetic Slices II - Subsequence C++ Python O(n^2) O(n * d) Hard
465 Optimal Account Balancing C++ Python O(n * 2^n) O(n * 2^n) Hard πŸ“–
466 Count The Repetitions C++ Python O(s1 * min(s2, n1)) O(s2) Hard
467 Unique Substrings in Wraparound String C++ Python O(n) O(1) Medium
471 Encode String with Shortest Length C++ Python O(n^3) on average O(n^2) Medium πŸ“–
472 Concatenated Words C++ Python O(n * l^2) O(n * l) Medium
474 Ones and Zeroes C++ Python O(s * m * n) O(m * n) Medium
486 Predict the Winner C++ Python O(n^2) O(n) Medium
514 Freedom Trail C++ Python O(k) ~ O(k * r^2) O(r) Hard
516 Longest Palindromic Subsequence C++ Python O(n^2) O(n) Medium
546 Remove Boxes C++ Python O(n^3) ~ O(n^4) O(n^3) Hard
552 Student Attendance Record II C++ Python O(n) O(1) Hard
562 Longest Line of Consecutive One in Matrix C++ Python O(m * n) O(n) Medium πŸ“–
568 Maximum Vacation Days C++ Python O(n^2 * k) O(k) Hard πŸ“–
576 Out of Boundary Paths C++ Python O(N * m * n) O(m * n) Medium
583 Delete Operation for Two Strings C++ Python O(m * n) O(n) Medium
600 Non-negative Integers without Consecutive Ones C++ Python O(1) O(1) Hard
629 K Inverse Pairs Array C++ Python O(n * k) O(k) Hard
639 Decode Ways II C++ Python O(n) O(1) Hard
650 2 Keys Keyboard C++ Python O(sqrt(n)) O(1) Medium
656 Coin Path C++ Python O(n * B) O(n) Hard πŸ“–
664 Strange Printer C++ Python O(n^3) O(n^2) Hard
673 Number of Longest Increasing Subsequence C++ Python O(n^2) O(n) Medium
688 Knight Probability in Chessboard C++ Python O(k * n^2) O(n^2) Medium
689 Maximum Sum of 3 Non-Overlapping Subarrays C++ Python O(n) O(n) Hard
691 Stickers to Spell Word C++ Python O(T * S^T) O(T * S^T) Hard Backtracking, Memoization
712 Minimum ASCII Delete Sum for Two Strings C++ Python O(m * n) O(n) Medium
714 Best Time to Buy and Sell Stock with Transaction Fee C++ Python O(n) O(1) Medium
727 Minimum Window Subsequence C++ Python O(s * t) O(s) Hard πŸ“–
730 Count Different Palindromic Subsequences C++ Python O(n^2) O(n) Hard
740 Delete and Earn C++ Python O(n) O(1) Medium
741 Cherry Pickup C++ Python O(n^3) O(n^2) Hard
746 Min Cost Climbing Stairs C++ Python O(n) O(1) Easy
750 Number Of Corner Rectangles C++ Python O(n * m^2) O(n * m) Medium
764 Largest Plus Sign C++ Python O(n^2) O(n^2) Medium
788 Rotated Digits C++ Python O(logn) O(logn) Easy Memoization
790 Domino and Tromino Tiling C++ Python O(logn) O(logn) Medium Matrix Exponentiation
799 Champagne Tower C++ Python O(n^2) O(n) Medium
801 Minimum Swaps To Make Sequences Increasing C++ Python O(n) O(1) Medium
805 Split Array With Same Average C++ Python O(n^4) O(n^3) Hard
808 Soup Servings C++ Python O(1) O(1) Medium Memoization
813 Largest Sum of Averages C++ Python O(k * n^2) O(n) Medium
818 Race Car C++ Python O(nlogn) O(n) Hard
823 Binary Trees With Factors C++ Python O(n^2) O(n) Medium
837 New 21 Game C++ Python O(n) O(n) Medium
838 Push Dominoes C++ Python O(n) O(n) Medium
847 Shortest Path Visiting All Nodes C++ Python O(n *2^n) O(n * 2^n) Hard BFS
877 Stone Game C++ Python O(n^2) O(n) Medium variant of Predict the Winner
879 Profitable Schemes C++ Python O(n * p * g) O(p * g) Hard
903 Valid Permutations for DI Sequence C++ Python O(n^2) O(n) Hard
920 Number of Music Playlists C++ Python O(n * l) O(l) Hard

Greedy

# Title Solution Time Space Difficulty Tag Note
011 Container With Most Water C++ Python O(n) O(1) Medium
042 Trapping Rain Water C++ Python O(n) O(1) Hard Tricky
044 Wildcard Matching Python O(m + n) O(1) Hard Tricky
045 Jump Game II Python O(n) O(1) Hard
055 Jump Game Python O(n) O(1) Medium
122 Best Time to Buy and Sell Stock II Python O(n) O(1) Easy
134 Gas Station Python O(n) O(1) Medium
135 Candy C++ Python O(n) O(n) Hard
316 Remove Duplicate Letters C++ Python O(n) O(k) Hard Ascending Stack
321 Create Maximum Number C++ Python O(k * (m + n + k)) ~ O(k * (m + n + k^2)) O(m + n + k^2) Hard variant of Delete Digits Greedy, DP
330 Patching Array C++ Python O(s + logn) O(1) Hard
376 Wiggle Subsequence C++ Python O(n) O(1) Medium
392 Is Subsequence C++ Python O(n) O(1) Medium
397 Integer Replacement C++ Python O(n) O(1) Medium Math
402 Remove K Digits C++ Python O(n) O(n) Medium LintCode
435 Non-overlapping Intervals C++ Python O(nlogn) O(1) Medium Line Sweep
452 Minimum Number of Arrows to Burst Balloons C++ Python O(nlogn) O(1) Medium
455 Assign Cookies C++ Python O(nlogn) O(1) Easy
621 Task Scheduler C++ Python O(n) O(1) Medium
630 Course Schedule III C++ Python O(nlogn) O(k) Hard
646 Maximum Length of Pair Chain C++ Python O(nlogn) O(1) Medium similar to Non-overlapping Intervals Line Sweep
649 Dota2 Senate C++ Python O(n) O(n) Medium
659 Split Array into Consecutive Subsequences C++ Python O(n) O(1) Medium
738 Monotone Increasing Digits C++ Python O(1) O(1) Medium
757 Set Intersection Size At Least Two C++ Python O(nlogn) O(n) Hard
759 Employee Free Time C++ Python O(m * logn) O(n) Hard
763 Partition Labels C++ Python O(n) O(n) Medium
767 Reorganize String C++ Python O(n) O(1) Medium
798 Smallest Rotation with Highest Score C++ Python O(n) O(1) Hard
843 Guess the Word C++ Python O(n^2) O(n) Hard MinMax
861 Score After Flipping Matrix C++ Python O(r * c) O(1) Medium
870 Advantage Shuffle C++ Python O(nlogn) O(n) Medium
881 Boats to Save People C++ Python O(nlogn) O(n) Medium

Graph

# Title Solution Time Space Difficulty Tag Note
765 Couples Holding Hands C++ Python O(n) O(n) Hard

Geometry

# Title Solution Time Space Difficulty Tag Note
587 Erect the Fence C++ Python O(nlogn) O(n) Hard Monotone Chain
892 Surface Area of 3D Shapes C++ Python O(n^2) O(1) Easy

Simulation

# Title Solution Time Space Difficulty Tag Note
874 Walking Robot Simulation C++ Python O(n + k) O(k) Easy

Design

# Title Solution Time Space Difficulty Tag Note
146 LRU Cache C++ Python O(1) O(k) Hard
225 Implement Stack using Queues C++ Python push: O(n), pop: O(1), top: O(1) O(n) Easy
284 Peeking Iterator C++ Python O(1) O(1) Medium
348 Design Tic-Tac-Toe C++ Python O(1) O(n^2) Medium πŸ“–
353 Design Snake Game C++ Python O(1) O(s) Medium πŸ“– Deque
355 Design Twitter C++ Python O(klogu) O(t + f) Medium LintCode Heap
359 Logger Rate Limiter C++ Python O(1), amortized O(k) Easy πŸ“– Deque
362 Design Hit Counter C++ Python O(1), amortized O(k) Medium πŸ“– Deque
379 Design Phone Directory C++ Python O(1) O(n) Medium πŸ“–
380 Insert Delete GetRandom O(1) C++ Python O(1) O(n) Hard
381 Insert Delete GetRandom O(1) - Duplicates allowed C++ Python O(1) O(n) Hard
432 All O`one Data Structure C++ Python O(1) O(n) Hard
460 LFU Cache C++ Python O(1) O(k) Hard
535 Encode and Decode TinyURL C++ Python O(1) O(n) Medium
588 Design In-Memory File System C++ Python ls: O(l + klogk)
mkdir: O(l)
addContentToFile: O(l + c)
readContentFromFile: O(l + c)
O(n + s) Hard πŸ“–
604 Design Compressed String Iterator C++ Python O(1) O(1) Easy πŸ“–
631 Design Excel Sum Formula C++ Python set: O((r * c)^2)
get: O(1)
sum: O((r * c)^2)
O(r * c) Hard πŸ“–
635 Design Log Storage System C++ Python put: O(1)
retrieve: O(n + dlogd)
O(n) Medium πŸ“–
642 Design Search Autocomplete System C++ Python O(p^2) O(p * t + s) Hard πŸ“–
715 Range Module C++ Python add: O(n)
remove: O(n)
query: O(logn)
O(n) Hard
716 Max Stack C++ Python push: O(logn)
pop: O(logn)
popMax: O(logn)
top: O(1)
peekMax: O(1)
O(n) Easy
745 Prefix and Suffix Search C++ Python ctor: O(w * l^2)
search : O(p + s)
O(t) Hard Trie
900 RLE Iterator C++ Python O(n) O(1) Medium

SQL

# Title Solution Time Space Difficulty Tag Note
175 Combine Two Tables MySQL O(m + n) O(m + n) Easy
176 Second Highest Salary MySQL O(n) O(1) Easy
177 Nth Highest Salary MySQL O(n^2) O(n) Medium
178 Rank Scores MySQL O(n^2) O(n) Medium
180 Consecutive Numbers MySQL O(n) O(n) Medium
181 Employees Earning More Than Their Managers MySQL O(n^2) O(1) Easy
182 Duplicate Emails MySQL O(n^2) O(n) Easy
183 Customers Who Never Order MySQL O(n^2) O(1) Easy
184 Department Highest Salary MySQL O(n^2) O(n) Medium
185 Department Top Three Salaries MySQL O(n^2) O(n) Hard
196 Delete Duplicate Emails MySQL O(n^2) O(n) Easy
197 Rising Temperature MySQL O(n^2) O(n) Easy
262 Trips and Users MySQL O((t * u) + tlogt) O(t) Hard

Shell Script

# Title Solution Time Space Difficulty Tag Note
192 Word Frequency Shell O(n) O(k) Medium
193 Valid Phone Numbers Shell O(n) O(1) Easy
194 Transpose File Shell O(n^2) O(n^2) Medium
195 Tenth Line Shell O(n) O(1) Easy

About

πŸ“ Python / C++ 11 Solutions of All LeetCode Questions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 58.2%
  • C++ 41.3%
  • Other 0.5%