Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

LeetCode Pattern 500 offers 500 solutions for LeetCode problems in Python and Java, 17 notes on essential concepts related to data structures and algorithms, and 130 patterns for solving LeetCode problems.

License

NotificationsYou must be signed in to change notification settings

tiationg-kho/leetcode-pattern-500

Repository files navigation

If you often find yourself grappling with how to tackle LeetCode problems, our repo is here to assist you!

LeetCode Pattern 500 is meticulously designed to facilitate your swift adaptation to diverse problem types and their numerous variations by discerning recurring patterns within these problems.

What is LeetCode Pattern 500

LeetCode Pattern 500 is inspired by Blind 75 and Grind 75's classic problem selection. LeetCode Pattern 500 offers 500 solutions for LeetCode problems in Python and Java, 17 notes on essential concepts related to data structures and algorithms, and 130 patterns for solving LeetCode problems. Most importantly, each solution provides analysis of which pattern to be used and the time/space complexity. This comprehensive resource can help you improve your coding skills, deepen your understanding of fundamental DSA topics, and prepare for SDE interviews.

Mind map for all 130 patterns in LeetCode Pattern 500

Check ourmind map in Figma.

Suggestions & Questions

Feel free to raise issues or post in discussions.

LeetCode Pattern 500

NumGrind 75NameTopicPatternSolution
001tree note[A]tree
002O100. Same Tree[A]tree-divide-and-conquertwo branch top-downPythonJava
003O101. Symmetric Tree[A]tree-divide-and-conquertwo branch top-downPythonJava
004O98. Validate Binary Search Tree[A]tree-divide-and-conquertwo branch top-downPythonJava
005O105. Construct Binary Tree from Preorder and Inorder Traversal[A]tree-divide-and-conquerre-build tree (top-down)PythonJava
006106. Construct Binary Tree from Inorder and Postorder Traversal[A]tree-divide-and-conquerre-build tree (top-down)PythonJava
007889. Construct Binary Tree from Preorder and Postorder Traversal[A]tree-divide-and-conquerre-build tree (top-down)Python
0081008. Construct Binary Search Tree from Preorder Traversal[A]tree-divide-and-conquerre-build BST (top-down approach)Python
009O108. Convert Sorted Array to Binary Search Tree[A]tree-divide-and-conquerre-build BST (top-down approach)PythonJava
010109. Convert Sorted List to Binary Search Tree[A]tree-divide-and-conquerre-build BST (inorder approach)Python
011700. Search in a Binary Search Tree[A]tree-divide-and-conqueruse BST attributesPython
012701. Insert into a Binary Search Tree[A]tree-divide-and-conqueruse BST attributesPython
013O285. Inorder Successor in BST[A]tree-divide-and-conqueruse BST attributesPython
014450. Delete Node in a BST[A]tree-divide-and-conqueruse BST attributesPython
015O235. Lowest Common Ancestor of a Binary Search Tree[A]tree-divide-and-conquerfind LCAPythonJava
0161650. Lowest Common Ancestor of a Binary Tree III[A]tree-divide-and-conquerfind LCAPython
017116. Populating Next Right Pointers in Each Node[A]tree-divide-and-conquerpopulate next ptrPython
018117. Populating Next Right Pointers in Each Node II[A]tree-divide-and-conquerpopulate next ptrPython
01996. Unique Binary Search Trees[A]tree-divide-and-conquerunique BSTPython
02095. Unique Binary Search Trees II[A]tree-divide-and-conquerunique BSTPython
021872. Leaf-Similar Trees[A]tree-dfs-preorderpreorderPython
022O226. Invert Binary Tree[A]tree-dfs-preorderpreorderPythonJava
0231448. Count Good Nodes in Binary Tree[A]tree-dfs-preorderpreorderPython
0241457. Pseudo-Palindromic Paths in a Binary Tree[A]tree-dfs-preorderpreorderPython
025O572. Subtree of Another Tree[A]tree-dfs-preorderturn tree to stringPythonJava
026606. Construct String from Binary Tree[A]tree-dfs-preorderturn tree to stringPython
027O297. Serialize and Deserialize Binary Tree[A]tree-dfs-preorderturn tree to stringPython
028449. Serialize and Deserialize BST[A]tree-dfs-preorderturn tree to stringPython
029545. Boundary of Binary Tree[A]tree-dfs-preorderrecord leavesPython
03094. Binary Tree Inorder Traversal[A]tree-dfs-inorderinorderPython
031O230. Kth Smallest Element in a BST[A]tree-dfs-inorderinorderPythonJava
032173. Binary Search Tree Iterator[A]tree-dfs-inorderinorderPython
03399. Recover Binary Search Tree[A]tree-dfs-inorderinorderPython
034366. Find Leaves of Binary Tree[A]tree-dfs-postorderpostorderPython
035O236. Lowest Common Ancestor of a Binary Tree[A]tree-dfs-postorderpostorderPythonJava
036O124. Binary Tree Maximum Path Sum[A]tree-dfs-postorderpostorderPython
037O543. Diameter of Binary Tree[A]tree-dfs-postorderpostorderPythonJava
038O110. Balanced Binary Tree[A]tree-dfs-postorderpostorderPythonJava
039865. Smallest Subtree with all the Deepest Nodes[A]tree-dfs-postorderpostorderPython
0401120. Maximum Average Subtree[A]tree-dfs-postorderpostorderPython
041298. Binary Tree Longest Consecutive Sequence[A]tree-dfs-postorderpostorderPython
0421644. Lowest Common Ancestor of a Binary Tree II[A]tree-dfs-postorderpostorderPython
0431145. Binary Tree Coloring Game[A]tree-dfs-postorderpostorderPython
0442246. Longest Path With Different Adjacent Characters[A]tree-dfs-postorderpostorderPython
045O102. Binary Tree Level Order Traversal[A]tree-bfsbfsPythonJava
046O103. Binary Tree Zigzag Level Order Traversal[A]tree-bfsbfsPython
047O199. Binary Tree Right Side View[A]tree-bfsbfsPythonJava
048O104. Maximum Depth of Binary Tree[A]tree-bfsbfsPythonJava
0492415. Reverse Odd Levels of Binary Tree[A]tree-bfsbfsPython
050623. Add One Row to Tree[A]tree-bfsbfsPython
051690. Employee Importance[A]tree-bfsbfsPython
052O863. All Nodes Distance K in Binary Tree[A]tree-bfsbuild a child_parent hashmapPython
0532385. Amount of Time for Binary Tree to Be Infected[A]tree-bfsbuild a child_parent hashmapPython
054O662. Maximum Width of Binary Tree[A]tree-bfsassign idxPython
055314. Binary Tree Vertical Order Traversal[A]tree-bfsassign coordinatesPython
056987. Vertical Order Traversal of a Binary Tree[A]tree-bfsassign coordinatesPython
057linked-list note[B]linked-list
058O21. Merge Two Sorted Lists[B]linked-listuse sentinel nodePythonJava
059O328. Odd Even Linked List[B]linked-listuse sentinel nodePython
060O2. Add Two Numbers[B]linked-listuse sentinel nodePythonJava
061O19. Remove Nth Node From End of List[B]linked-listuse sentinel nodePython
062369. Plus One Linked List[B]linked-listuse sentinel nodePython
063O141. Linked List Cycle[B]linked-listuse two pointers (slow and fast)PythonJava
064142. Linked List Cycle II[B]linked-listuse two pointers (slow and fast)Python
065O876. Middle of the Linked List[B]linked-listuse two pointers (slow and fast)PythonJava
066O206. Reverse Linked List[B]linked-listuse two pointers (prev and cur)PythonJava
06792. Reverse Linked List II[B]linked-listuse two pointers (prev and cur)Python
068O25. Reverse Nodes in k-Group[B]linked-listuse two pointers (prev and cur)Python
0692074. Reverse Nodes in Even Length Groups[B]linked-listuse two pointers (prev and cur)Python
070O24. Swap Nodes in Pairs[B]linked-listuse two pointers (prev and cur)Python
0711836. Remove Duplicates From an Unsorted Linked List[B]linked-listuse two pointers (prev and cur)Python
07283. Remove Duplicates from Sorted List[B]linked-listuse two pointers (prev and cur)Python
07382. Remove Duplicates from Sorted List II[B]linked-listuse two pointers (prev and cur)Python
074160. Intersection of Two Linked Lists[B]linked-listuse two pointers (find LCA/intersection)Python
075O234. Palindrome Linked List[B]linked-listuse two pointers (utilize symmetry property)PythonJava
076O143. Reorder List[B]linked-listuse two pointers (utilize symmetry property)Python
0772130. Maximum Twin Sum of a Linked List[B]linked-listuse two pointers (utilize symmetry property)Python
078O61. Rotate List[B]linked-listget linked list lengthPython
0791019. Next Greater Node In Linked List[B]linked-listget linked list lengthPython
080O148. Sort List[B]linked-listuse merge sort to sort listPython
081138. Copy List with Random Pointer[B]linked-listinterweaving nodesPython
082O146. LRU Cache[B]linked-listuse dll and hashmap togetherPythonJava
083460. LFU Cache[B]linked-listuse dll and hashmap togetherPython
084432. All O`one Data Structure[B]linked-listuse dll and hashmap togetherPython
085237. Delete Node in a Linked List[B]linked-listchange val as change nodePython
0861206. Design Skiplist[B]linked-listskiplistPython
087hashmap note[C]hashmap
088705. Design HashSet[C]hashmapseparate chainingPython
089706. Design HashMap[C]hashmapseparate chainingPython
0901805. Number of Different Integers in a String[C]hashmapstore valPython
091O217. Contains Duplicate[C]hashmapstore valPythonJava
092O128. Longest Consecutive Sequence[C]hashmapstore valPythonJava
093O13. Roman to Integer[C]hashmapstore valPythonJava
094953. Verifying an Alien Dictionary[C]hashmapstore valPython
0952342. Max Sum of a Pair With Equal Sum of Digits[C]hashmapstore valPython
096O336. Palindrome Pairs[C]hashmapstore valPython
097O1. Two Sum[C]hashmapstore valPythonJava
098O380. Insert Delete GetRandom O(1)[C]hashmapstore valPython
099381. Insert Delete GetRandom O(1) - Duplicates allowed[C]hashmapstore valPython
100734. Sentence Similarity[C]hashmapstore valPython
101609. Find Duplicate File in System[C]hashmapstore valPython
102249. Group Shifted Strings[C]hashmapstore valPython
103O383. Ransom Note[C]hashmapstore sth’s freqPythonJava
104O242. Valid Anagram[C]hashmapstore sth’s freqPythonJava
105O409. Longest Palindrome[C]hashmapstore sth’s freqPythonJava
106387. First Unique Character in a String[C]hashmapstore sth’s freqPythonJava
107767. Reorganize String[C]hashmapstore sth’s freqPython
108266. Palindrome Permutation[C]hashmapstore sth’s freqPython
1092423. Remove Letter To Equalize Frequency[C]hashmapstore sth’s freqPython
110299. Bulls and Cows[C]hashmapstore sth’s freqPython
111819. Most Common Word[C]hashmapstore sth’s freqPython
112O49. Group Anagrams[C]hashmapstore sth’s freqPythonJava
1131152. Analyze User Website Visit Pattern[C]hashmapstore sth’s freqPython
114828. Count Unique Characters of All Substrings of a Given String[C]hashmapsnapshot of hashmapPython
1151055. Shortest Way to Form String[C]hashmapsnapshot of hashmapPython
116205. Isomorphic Strings[C]hashmapbuild bijection mapping relationPythonJava
117890. Find and Replace Pattern[C]hashmapbuild bijection mapping relationPython
1181010. Pairs of Songs With Total Durations Divisible by 60[C]hashmapstore valid val’s freq for finding pairsPython
1192364. Count Number of Bad Pairs[C]hashmapstore valid val’s freq for finding pairsPython
1202013. Detect Squares[C]hashmapstore valid val’s freq for finding pairsPython
121binary-search note[D]binary-search
122O704. Binary Search[D]binary-searchsearch in a sorted array for specific valPythonJava
123367. Valid Perfect Square[D]binary-searchsearch in a sorted array for specific valPython
124O74. Search a 2D Matrix[D]binary-searchsearch in a sorted array for specific valPythonJava
12534. Find First and Last Position of Element in Sorted Array[D]binary-searchsearch in a sorted array for specific valPythonJava
126702. Search in a Sorted Array of Unknown Size[D]binary-searchsearch in a sorted array for specific valPython
12735. Search Insert Position[D]binary-searchsearch in a sorted array for most close valPythonJava
1281428. Leftmost Column with at Least a One[D]binary-searchsearch in a sorted array for most close valPython
129744. Find Smallest Letter Greater Than Target[D]binary-searchsearch in a sorted array for most close valPython
130O981. Time Based Key-Value Store[D]binary-searchsearch in a sorted array for most close valPythonJava
131O528. Random Pick with Weight[D]binary-searchsearch in a sorted array for most close valPython
132O278. First Bad Version[D]binary-searchsearch in sth’s rangePythonJava
133222. Count Complete Tree Nodes[D]binary-searchsearch in sth’s rangePython
134878. Nth Magical Number[D]binary-searchsearch in sth’s rangePython
1351201. Ugly Number III[D]binary-searchsearch in sth’s rangePython
136O4. Median of Two Sorted Arrays[D]binary-searchsearch in sth’s rangePython
1371062. Longest Repeating Substring[D]binary-searchsearch in sth’s rangePython
1381011. Capacity To Ship Packages Within D Days[D]binary-searchsearch in sth’s rangePython
139875. Koko Eating Bananas[D]binary-searchsearch in sth’s rangePython
140719. Find K-th Smallest Pair Distance[D]binary-searchsearch in sth’s rangePython
141852. Peak Index in a Mountain Array[D]binary-searchuse boundary to recordPython
142O658. Find K Closest Elements[D]binary-searchuse boundary to recordPython
143162. Find Peak Element[D]binary-searchuse boundary to recordPython
144O33. Search in Rotated Sorted Array[D]binary-searchrotated sorted arrayPythonJava
14581. Search in Rotated Sorted Array II[D]binary-searchrotated sorted arrayPython
146O153. Find Minimum in Rotated Sorted Array[D]binary-searchrotated sorted arrayPython
147heap note[E]heap
148O1235. Maximum Profit in Job Scheduling[E]heapgreedily schedule tasks (start/end/val)Python
149646. Maximum Length of Pair Chain[E]heapgreedily schedule tasks (start/end/val)Python
1502008. Maximum Earnings From Taxi[E]heapgreedily schedule tasks (start/end/val)Python
1512054. Two Best Non-Overlapping Events[E]heapgreedily schedule tasks (start/end/val)Python
152O973. K Closest Points to Origin[E]heaptop k problem (based on heap)PythonJava
153O692. Top K Frequent Words[E]heaptop k problem (based on heap)Python
154264. Ugly Number II[E]heapk way merge problemPython
155313. Super Ugly Number[E]heapk way merge problemPython
156355. Design Twitter[E]heapk way merge problemPython
157O759. Employee Free Time[E]heapk way merge problemPython
158O632. Smallest Range Covering Elements from K Lists[E]heapk way merge problemPython
159O23. Merge k Sorted Lists[E]heapk way merge problemPython
160O295. Find Median from Data Stream[E]heaptwo heap problemPython
161502. IPO[E]heaptwo heap problemPython
162480. Sliding Window Median[E]heaptwo heap problemPython
1631383. Maximum Performance of a Team[E]heapstoring and popping out elementsPython
164630. Course Schedule III[E]heapstoring and popping out elementsPython
1651094. Car Pooling[E]heapstoring and popping out elementsPython
1661705. Maximum Number of Eaten Apples[E]heapstoring and popping out elementsPython
1671792. Maximum Average Pass Ratio[E]heapstoring and popping out elementsPython
168378. Kth Smallest Element in a Sorted Matrix[E]heapuse bfs and heapPython
169373. Find K Pairs with Smallest Sums[E]heapuse bfs and heapPython
170407. Trapping Rain Water II[E]heapuse bfs and heapPython
171string note[F]string
172O844. Backspace String Compare[F]stringtraverse from endPythonJava
17343. Multiply Strings[F]stringtraverse from endPython
17458. Length of Last Word[F]stringtraverse from endPython
175O8. String to Integer (atoi)[F]stringhandle value’s boundPythonJava
176O271. Encode and Decode Strings[F]stringuse chunkPython
17728. Find the Index of the First Occurrence in a String[F]stringuse rabin karp (rolling hash)Python
178187. Repeated DNA Sequences[F]stringuse rabin karp (rolling hash)Python
1791044. Longest Duplicate Substring[F]stringuse rabin karp (rolling hash)Python
180718. Maximum Length of Repeated Subarray[F]stringuse rabin karp (rolling hash)Python
181O14. Longest Common Prefix[F]stringstring compositionPythonJava
182273. Integer to English Words[F]stringstring compositionPython
18368. Text Justification[F]stringstring compositionPython
18465. Valid Number[F]stringstring compositionPython
1856. Zigzag Conversion[F]stringstring compositionPython
186trie note[G]trie
187O208. Implement Trie (Prefix Tree)[G]triestandard triePythonJava
188O588. Design In-Memory File System[G]triecustom trie nodePython
1891268. Search Suggestions System[G]triecustom trie nodePython
1902185. Counting Words With a Given Prefix[G]triecustom trie nodePython
1912416. Sum of Prefix Scores of Strings[G]triecustom trie nodePython
192745. Prefix and Suffix Search[G]triecustom trie nodePython
1931166. Design File System[G]triecustom trie nodePython
194O211. Design Add and Search Words Data Structure[G]trieperform dfs inside triePython
195472. Concatenated Words[G]trieperform dfs inside triePython
196642. Design Search Autocomplete System[G]trieperform dfs inside triePython
197array note[H]array
198243. Shortest Word Distance[H]arraytraversePython
199O169. Majority Element[H]arrayuse boyer moore vote algorithmPythonJava
200229. Majority Element II[H]arrayuse boyer moore vote algorithmPython
201O189. Rotate Array[H]arrayuse reverse techniquePython
202O362. Design Hit Counter[H]arrayuse circular arrayPython
203O41. First Missing Positive[H]arrayspecific range array (cyclic sort)Python
204442. Find All Duplicates in an Array[H]arrayspecific range array (cyclic sort)Python
205448. Find All Numbers Disappeared in an Array[H]arrayspecific range array (cyclic sort)Python
206O287. Find the Duplicate Number[H]arrayspecific range array (cycle detection)Python
207845. Longest Mountain in Array[H]arrayuse finite state machinePython
208370. Range Addition[H]arrayuse difference arrayPython
209849. Maximize Distance to Closest Person[H]arraycount continuous elementsPython
210384. Shuffle an Array[H]arrayuse knuth shufflePython
211398. Random Pick Index[H]arrayuse reservoir samplingPython
2121535. Find the Winner of an Array Game[H]arraysimulationPython
213280. Wiggle Sort[H]arrayuse swapPython
2141472. Design Browser History[H]arraymaintain array's range dynamicallyPython
215163. Missing Ranges[H]arraypre-process the arrayPython
216O56. Merge Intervals[H]array-line-sweepcompare two intervals each roundPythonJava
217O57. Insert Interval[H]array-line-sweepcompare two intervals each roundPythonJava
2181272. Remove Interval[H]array-line-sweepcompare two intervals each roundPython
219O252. Meeting Rooms[H]array-line-sweepcompare two intervals each roundPythonJava
220O435. Non-overlapping Intervals[H]array-line-sweepcompare two intervals each roundPython
2211288. Remove Covered Intervals[H]array-line-sweepcompare two intervals each roundPython
222452. Minimum Number of Arrows to Burst Balloons[H]array-line-sweepcompare two intervals each roundPython
223757. Set Intersection Size At Least Two[H]array-line-sweepcompare two intervals each roundPython
224986. Interval List Intersections[H]array-line-sweepcompare two intervals each roundPython
2251229. Meeting Scheduler[H]array-line-sweepcompare two intervals each roundPython
226O253. Meeting Rooms II[H]array-line-sweepuse heap to store previous intervals’ statesPython
2271851. Minimum Interval to Include Each Query[H]array-line-sweepuse heap to store previous intervals’ statesPython
228303. Range Sum Query - Immutable[H]array-prefix-sumuse standard prefix sumPython
229304. Range Sum Query 2D - Immutable[H]array-prefix-sumuse standard prefix sumPython
230O238. Product of Array Except Self[H]array-prefix-sumuse standard prefix sumPythonJava
231724. Find Pivot Index[H]array-prefix-sumuse standard prefix sumPython
2322256. Minimum Average Difference[H]array-prefix-sumuse standard prefix sumPython
2332100. Find Good Days to Rob the Bank[H]array-prefix-sumuse standard prefix sumPython
2342055. Plates Between Candles[H]array-prefix-sumuse standard prefix sumPython
2351031. Maximum Sum of Two Non-Overlapping Subarrays[H]array-prefix-sumuse standard prefix sumPython
236O560. Subarray Sum Equals K[H]array-prefix-sumuse hashmap to validate the gap subarrayPythonJava
237O525. Contiguous Array[H]array-prefix-sumuse hashmap to validate the gap subarrayPython
238974. Subarray Sums Divisible by K[H]array-prefix-sumuse hashmap to validate the gap subarrayPython
239523. Continuous Subarray Sum[H]array-prefix-sumuse hashmap to validate the gap subarrayPython
240325. Maximum Size Subarray Sum Equals k[H]array-prefix-sumuse hashmap to validate the gap subarrayPython
2411248. Count Number of Nice Subarrays[H]array-prefix-sumuse hashmap to validate the gap subarrayPython
242363. Max Sum of Rectangle No Larger Than K[H]array-prefix-sumuse hashmap to validate the gap subarrayPython
243548. Split Array with Equal Sum[H]array-prefix-sumuse hashmap to validate the gap subarrayPython
244O3. Longest Substring Without Repeating Characters[H]array-sliding-windowuse standard sliding windowPythonJava
245567. Permutation in String[H]array-sliding-windowuse standard sliding windowPython
246O438. Find All Anagrams in a String[H]array-sliding-windowuse standard sliding windowPythonJava
247O424. Longest Repeating Character Replacement[H]array-sliding-windowuse standard sliding windowPythonJava
2481151. Minimum Swaps to Group All 1's Together[H]array-sliding-windowuse standard sliding windowPython
2492134. Minimum Swaps to Group All 1's Together II[H]array-sliding-windowuse standard sliding windowPython
250340. Longest Substring with At Most K Distinct Characters[H]array-sliding-windowuse standard sliding windowPython
2511100. Find K-Length Substrings With No Repeated Characters[H]array-sliding-windowuse standard sliding windowPython
252904. Fruit Into Baskets[H]array-sliding-windowuse standard sliding windowPython
2531423. Maximum Points You Can Obtain from Cards[H]array-sliding-windowuse standard sliding windowPython
25430. Substring with Concatenation of All Words[H]array-sliding-windowuse standard sliding windowPython
255395. Longest Substring with At Least K Repeating Characters[H]array-sliding-windowuse standard sliding windowPython
256O76. Minimum Window Substring[H]array-sliding-windowuse shrink type sliding windowPython
257209. Minimum Size Subarray Sum[H]array-sliding-windowuse shrink type sliding windowPython
258O121. Best Time to Buy and Sell Stock[H]array-two-pointers-same-directionuse left ptr to recordPythonJava
259122. Best Time to Buy and Sell Stock II[H]array-two-pointers-same-directionuse left ptr to recordPython
260O283. Move Zeroes[H]array-two-pointers-same-directionuse left ptr to recordPythonJava
26127. Remove Element[H]array-two-pointers-same-directionuse left ptr to recordPython
26226. Remove Duplicates from Sorted Array[H]array-two-pointers-same-directionuse left ptr to recordPython
26380. Remove Duplicates from Sorted Array II[H]array-two-pointers-same-directionuse left ptr to recordPython
264O75. Sort Colors[H]array-two-pointers-same-directionuse left ptr to recordPythonJava
2652414. Length of the Longest Alphabetical Continuous Substring[H]array-two-pointers-same-directionuse left ptr to recordPython
266O31. Next Permutation[H]array-two-pointers-same-directionfind next permutationPython
267556. Next Greater Element III[H]array-two-pointers-same-directionfind next permutationPython
268165. Compare Version Numbers[H]array-two-pointers-same-directiontraverse two sequencesPython
2692337. Move Pieces to Obtain a String[H]array-two-pointers-same-directiontraverse two sequencesPython
2701574. Shortest Subarray to be Removed to Make Array Sorted[H]array-two-pointers-same-directiontraverse two sequencesPython
271244. Shortest Word Distance II[H]array-two-pointers-same-directiontraverse two sequencesPython
272167. Two Sum II - Input Array Is Sorted[H]array-two-pointers-opposite-directionuse shrink typePython
273O15. 3Sum[H]array-two-pointers-opposite-directionuse shrink typePythonJava
27418. 4Sum[H]array-two-pointers-opposite-directionuse shrink typePython
275O16. 3Sum Closest[H]array-two-pointers-opposite-directionuse shrink typePython
276O977. Squares of a Sorted Array[H]array-two-pointers-opposite-directionuse shrink typePythonJava
277O11. Container With Most Water[H]array-two-pointers-opposite-directionuse shrink typePythonJava
278O42. Trapping Rain Water[H]array-two-pointers-opposite-directionuse shrink typePython
279948. Bag of Tokens[H]array-two-pointers-opposite-directionuse shrink typePython
280O125. Valid Palindrome[H]array-two-pointers-opposite-directionuse shrink typePythonJava
281680. Valid Palindrome II[H]array-two-pointers-opposite-directionuse shrink typePython
282344. Reverse String[H]array-two-pointers-opposite-directionuse shrink typePythonJava
283151. Reverse Words in a String[H]array-two-pointers-opposite-directionuse shrink typePython
284O5. Longest Palindromic Substring[H]array-two-pointers-opposite-directionuse expand typePythonJava
285O179. Largest Number[H]array-sortuse self defined sortPython
286O215. Kth Largest Element in an Array[H]array-sorttop k problem (based on sort)PythonJava
287347. Top K Frequent Elements[H]array-sorttop k problem (based on sort)PythonJava
2881985. Find the Kth Largest Integer in the Array[H]array-sorttop k problem (based on sort)Python
2891710. Maximum Units on a Truck[H]array-sortuse bucket sortPython
290451. Sort Characters By Frequency[H]array-sortuse bucket sortPython
2911099. Two Sum Less Than K[H]array-sortuse bucket sortPython
292164. Maximum Gap[H]array-sortuse bucket sortPython
293462. Minimum Moves to Equal Array Elements II[H]array-sortuse quick selectPython
294324. Wiggle Sort II[H]array-sortuse quick selectPython
295315. Count of Smaller Numbers After Self[H]array-sortuse merge sortPython
296493. Reverse Pairs[H]array-sortuse merge sortPython
297stack-queue note[I]stack-queue
298346. Moving Average from Data Stream[I]stack-queueuse queue to simulatePython
299O20. Valid Parentheses[I]stack-queueuse stack to store the last statesPythonJava
300O150. Evaluate Reverse Polish Notation[I]stack-queueuse stack to store the last statesPythonJava
301O224. Basic Calculator[I]stack-queueuse stack to store the last statesPython
302O227. Basic Calculator II[I]stack-queueuse stack to store the last statesPython
303772. Basic Calculator III[I]stack-queueuse stack to store the last statesPython
304O735. Asteroid Collision[I]stack-queueuse stack to store the last statesPython
305O394. Decode String[I]stack-queueuse stack to store the last statesPython
3061209. Remove All Adjacent Duplicates in String II[I]stack-queueuse stack to store the last statesPython
307726. Number of Atoms[I]stack-queueuse stack to store the last statesPython
30871. Simplify Path[I]stack-queueuse stack to store the last statesPython
309O232. Implement Queue using Stacks[I]stack-queueimplement stack/queuePythonJava
310225. Implement Stack using Queues[I]stack-queueimplement stack/queuePython
311O155. Min Stack[I]stack-queueimplement stack/queuePythonJava
312716. Max Stack[I]stack-queueimplement stack/queuePython
313O895. Maximum Frequency Stack[I]stack-queueimplement stack/queuePython
314O32. Longest Valid Parentheses[I]stack-queueuse variables to simulate stackPython
3151249. Minimum Remove to Make Valid Parentheses[I]stack-queueuse variables to simulate stackPython
3162296. Design a Text Editor[I]stack-queueuse stack to simulatePython
317341. Flatten Nested List Iterator[I]stack-queueuse stack to simulatePython
318O239. Sliding Window Maximum[I]stack-queue-monotonicuse monotonic queue and sliding windowPython
3191438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit[I]stack-queue-monotonicuse monotonic queue and sliding windowPython
3201696. Jump Game VI[I]stack-queue-monotonicuse monotonic queue and sliding windowPython
321862. Shortest Subarray with Sum at Least K[I]stack-queue-monotonicuse monotonic queue and sliding windowPython
322O739. Daily Temperatures[I]stack-queue-monotonicuse monotonic stack (consider one side’s relationship)PythonJava
323496. Next Greater Element I[I]stack-queue-monotonicuse monotonic stack (consider one side’s relationship)Python
324503. Next Greater Element II[I]stack-queue-monotonicuse monotonic stack (consider one side’s relationship)Python
325402. Remove K Digits[I]stack-queue-monotonicuse monotonic stack (consider one side’s relationship)Python
326316. Remove Duplicate Letters[I]stack-queue-monotonicuse monotonic stack (consider one side’s relationship)Python
3271762. Buildings With an Ocean View[I]stack-queue-monotonicuse monotonic stack (consider one side’s relationship)Python
3281944. Number of Visible People in a Queue[I]stack-queue-monotonicuse monotonic stack (consider one side’s relationship)Python
3292345. Finding the Number of Visible Mountains[I]stack-queue-monotonicuse monotonic stack (consider one side’s relationship)Python
330768. Max Chunks To Make Sorted II[I]stack-queue-monotonicuse monotonic stack (consider one side’s relationship)Python
3312289. Steps to Make Array Non-decreasing[I]stack-queue-monotonicuse monotonic stack (consider one side’s relationship)Python
332O84. Largest Rectangle in Histogram[I]stack-queue-monotonicuse monotonic stack (consider two side’s relationship)Python
33385. Maximal Rectangle[I]stack-queue-monotonicuse monotonic stack (consider two side’s relationship)Python
334907. Sum of Subarray Minimums[I]stack-queue-monotonicuse monotonic stack (consider two side’s relationship)Python
3352104. Sum of Subarray Ranges[I]stack-queue-monotonicuse monotonic stack (consider two side’s relationship)Python
3361856. Maximum Subarray Min-Product[I]stack-queue-monotonicuse monotonic stack (consider two side’s relationship)Python
337backtracking note[J]backtracking
338O78. Subsets[J]backtrackingsubsetPythonJava
339O39. Combination Sum[J]backtrackingsubsetPythonJava
34090. Subsets II[J]backtrackingsubsetPython
34140. Combination Sum II[J]backtrackingsubsetPython
342O46. Permutations[J]backtrackingpermutationPythonJava
34347. Permutations II[J]backtrackingpermutationPython
344267. Palindrome Permutation II[J]backtrackingpermutationPython
34577. Combinations[J]backtrackingcombinationPython
346216. Combination Sum III[J]backtrackingcombinationPython
347O17. Letter Combinations of a Phone Number[J]backtrackingbacktracking with constraintsPythonJava
348O22. Generate Parentheses[J]backtrackingbacktracking with constraintsPython
349O113. Path Sum II[J]backtrackingbacktracking with constraintsPython
350O437. Path Sum III[J]backtrackingbacktracking with constraintsPython
351O79. Word Search[J]backtrackingbacktracking with constraintsPythonJava
352O212. Word Search II[J]backtrackingbacktracking with constraintsPython
353O37. Sudoku Solver[J]backtrackingbacktracking with constraintsPython
354O51. N-Queens[J]backtrackingbacktracking with constraintsPython
355465. Optimal Account Balancing[J]backtrackingbacktracking with constraintsPython
35693. Restore IP Addresses[J]backtrackingbacktracking with constraintsPython
357140. Word Break II[J]backtrackingbacktracking with constraintsPython
358257. Binary Tree Paths[J]backtrackingbacktracking with constraintsPython
359131. Palindrome Partitioning[J]backtrackingbacktracking with constraintsPython
360graph note[K]graph
361O329. Longest Increasing Path in a Matrix[K]graph-dfsdfsPython
3621059. All Paths from Source Lead to Destination[K]graph-dfsdfsPython
363O200. Number of Islands[K]graph-bfsbfs with single sourcePythonJava
364O733. Flood Fill[K]graph-bfsbfs with single sourcePythonJava
365O1730. Shortest Path to Get Food[K]graph-bfsbfs with single sourcePython
366O1197. Minimum Knight Moves[K]graph-bfsbfs with single sourcePython
367O261. Graph Valid Tree[K]graph-bfsbfs with single sourcePython
368O127. Word Ladder[K]graph-bfsbfs with single sourcePython
369O133. Clone Graph[K]graph-bfsbfs with single sourcePythonJava
370O815. Bus Routes[K]graph-bfsbfs with single sourcePython
371695. Max Area of Island[K]graph-bfsbfs with single sourcePython
3721905. Count Sub Islands[K]graph-bfsbfs with single sourcePython
3731345. Jump Game IV[K]graph-bfsbfs with single sourcePython
3741091. Shortest Path in Binary Matrix[K]graph-bfsbfs with single sourcePython
3751992. Find All Groups of Farmland[K]graph-bfsbfs with single sourcePython
376909. Snakes and Ladders[K]graph-bfsbfs with single sourcePython
377749. Contain Virus[K]graph-bfsbfs with single sourcePython
378694. Number of Distinct Islands[K]graph-bfsbfs with single sourcePython
379O542. 01 Matrix[K]graph-bfsbfs with multiple sourcesPythonJava
380O417. Pacific Atlantic Water Flow[K]graph-bfsbfs with multiple sourcesPython
381O994. Rotting Oranges[K]graph-bfsbfs with multiple sourcesPythonJava
382286. Walls and Gates[K]graph-bfsbfs with multiple sourcesPython
383130. Surrounded Regions[K]graph-bfsbfs with multiple sourcesPython
384126. Word Ladder II[K]graph-bfsbfs with hashset as queuePython
385301. Remove Invalid Parentheses[K]graph-bfsbfs with hashset as queuePython
386O721. Accounts Merge[K]graph-union-findunion findPythonJava
387O323. Number of Connected Components in an Undirected Graph[K]graph-union-findunion findPython
388547. Number of Provinces[K]graph-union-findunion findPython
3892316. Count Unreachable Pairs of Nodes in an Undirected Graph[K]graph-union-findunion findPython
390399. Evaluate Division[K]graph-union-findunion findPython
391305. Number of Islands II[K]graph-union-findunion findPython
3921101. The Earliest Moment When Everyone Become Friends[K]graph-union-findunion findPython
3932421. Number of Good Paths[K]graph-union-findunion findPython
3941102. Path With Maximum Minimum Value[K]graph-union-findunion findPython
395737. Sentence Similarity II[K]graph-union-findunion findPython
396O207. Course Schedule[K]graph-kahnkahn (topological sort)PythonJava
397O210. Course Schedule II[K]graph-kahnkahn (topological sort)Python
398O269. Alien Dictionary[K]graph-kahnkahn (topological sort)Python
399O310. Minimum Height Trees[K]graph-kahnkahn (topological sort)PythonJava
4002115. Find All Possible Recipes from Given Supplies[K]graph-kahnkahn (topological sort)Python
401802. Find Eventual Safe States[K]graph-kahnkahn (topological sort)Python
4021136. Parallel Courses[K]graph-kahnkahn (topological sort)Python
4032050. Parallel Courses III[K]graph-kahnkahn (topological sort)Python
4041203. Sort Items by Groups Respecting Dependencies[K]graph-kahnkahn (topological sort)Python
4052192. All Ancestors of a Node in a Directed Acyclic Graph[K]graph-kahnkahn (topological sort)Python
406743. Network Delay Time[K]graph-dijkstradijkstra (shortest path)Python
407778. Swim in Rising Water[K]graph-dijkstradijkstra (shortest path)Python
4081514. Path with Maximum Probability[K]graph-dijkstradijkstra (shortest path)Python
409505. The Maze II[K]graph-dijkstradijkstra (shortest path)Python
4102093. Minimum Cost to Reach City With Discounts[K]graph-dijkstradijkstra (shortest path)Python
411O787. Cheapest Flights Within K Stops[K]graph-bellman-fordbellman ford (shortest path)Python
4121334. Find the City With the Smallest Number of Neighbors at a Threshold Distance[K]graph-floyd-warshallfloyd warshall (shortest path)Python
4131135. Connecting Cities With Minimum Cost[K]graph-kruskalkruskal (mst)Python
4141584. Min Cost to Connect All Points[K]graph-kruskalkruskal (mst)Python
4151168. Optimize Water Distribution in a Village[K]graph-kruskalkruskal (mst)Python
4161489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree[K]graph-kruskalkruskal (mst)Python
4171724. Checking Existence of Edge Length Limited Paths II[K]graph-kruskalkruskal (mst)Python
4181192. Critical Connections in a Network[K]graph-tarjantarjan (scc)Python
419332. Reconstruct Itinerary[K]graph-hierholzerhierholzer (eulerian path)Python
420753. Cracking the Safe[K]graph-hierholzerhierholzer (eulerian path)Python
421O54. Spiral Matrix[K]graph-matrixmatrixPythonJava
422O48. Rotate Image[K]graph-matrixmatrixPython
423O73. Set Matrix Zeroes[K]graph-matrixmatrixPython
4241582. Special Positions in a Binary Matrix[K]graph-matrixmatrixPython
425348. Design Tic-Tac-Toe[K]graph-matrixmatrixPython
426bit-manipulation note[L]bit-manipulation
427O136. Single Number[L]bit-manipulationxorPythonJava
428O268. Missing Number[L]bit-manipulationxorPythonJava
429O191. Number of 1 Bits[L]bit-manipulationshiftPythonJava
430O190. Reverse Bits[L]bit-manipulationshiftPythonJava
4311356. Sort Integers by The Number of 1 Bits[L]bit-manipulationshiftPython
432O36. Valid Sudoku[L]bit-manipulationbitmaskingPython
4331915. Number of Wonderful Substrings[L]bit-manipulationbitmaskingPython
4341542. Find Longest Awesome Substring[L]bit-manipulationbitmaskingPython
435dynamic-programming note[M]dynamic-programming
436O62. Unique Paths[M]dynamic-programming2DPythonJava
43763. Unique Paths II[M]dynamic-programming2DPython
438O221. Maximal Square[M]dynamic-programming2DPython
43964. Minimum Path Sum[M]dynamic-programming2DPython
440688. Knight Probability in Chessboard[M]dynamic-programming2DPython
441O416. Partition Equal Subset Sum[M]dynamic-programmingknapsack (0-1 knapsack)PythonJava
442494. Target Sum[M]dynamic-programmingknapsack (0-1 knapsack)Python
4431049. Last Stone Weight II[M]dynamic-programmingknapsack (0-1 knapsack)Python
444474. Ones and Zeroes[M]dynamic-programmingknapsack (0-1 knapsack)Python
445O322. Coin Change[M]dynamic-programmingknapsack (complete knapsack)PythonJava
446279. Perfect Squares[M]dynamic-programmingknapsack (complete knapsack)Python
447518. Coin Change II[M]dynamic-programmingknapsack (combination knapsack)Python
448O377. Combination Sum IV[M]dynamic-programmingknapsack (permutation knapsack)Python
449O70. Climbing Stairs[M]dynamic-programminglinear sequencePythonJava
450O55. Jump Game[M]dynamic-programminglinear sequencePython
451O198. House Robber[M]dynamic-programminglinear sequencePythonJava
452213. House Robber II[M]dynamic-programminglinear sequencePython
453O152. Maximum Product Subarray[M]dynamic-programminglinear sequencePython
454O91. Decode Ways[M]dynamic-programminglinear sequencePython
455O139. Word Break[M]dynamic-programminglinear sequencePythonJava
456256. Paint House[M]dynamic-programminglinear sequencePython
457123. Best Time to Buy and Sell Stock III[M]dynamic-programminglinear sequencePython
458309. Best Time to Buy and Sell Stock with Cooldown[M]dynamic-programminglinear sequencePython
459376. Wiggle Subsequence[M]dynamic-programminglinear sequencePython
460487. Max Consecutive Ones II[M]dynamic-programminglinear sequencePython
461368. Largest Divisible Subset[M]dynamic-programminglinear sequencePython
4621105. Filling Bookcase Shelves[M]dynamic-programminglinear sequencePython
463O300. Longest Increasing Subsequence[M]dynamic-programmingLISPython
4641964. Find the Longest Valid Obstacle Course at Each Position[M]dynamic-programmingLISPython
465354. Russian Doll Envelopes[M]dynamic-programmingLISPython
4661143. Longest Common Subsequence[M]dynamic-programmingdouble sequencePython
46772. Edit Distance[M]dynamic-programmingdouble sequencePython
46897. Interleaving String[M]dynamic-programmingdouble sequencePython
469115. Distinct Subsequences[M]dynamic-programmingdouble sequencePython
4701092. Shortest Common Supersequence[M]dynamic-programmingdouble sequencePython
47110. Regular Expression Matching[M]dynamic-programmingdouble sequencePython
472410. Split Array Largest Sum[M]dynamic-programminginterval (start from one interval)Python
4731335. Minimum Difficulty of a Job Schedule[M]dynamic-programminginterval (start from one interval)Python
4741278. Palindrome Partitioning III[M]dynamic-programminginterval (start from one interval)Python
475312. Burst Balloons[M]dynamic-programminginterval (start from short interval)Python
476516. Longest Palindromic Subsequence[M]dynamic-programminginterval (start from short interval)Python
477647. Palindromic Substrings[M]dynamic-programminginterval (start from short interval)Python
4781547. Minimum Cost to Cut a Stick[M]dynamic-programminginterval (start from short interval)Python
479greedy note[N]greedy
480O53. Maximum Subarray[N]greedygreedyPythonJava
481O134. Gas Station[N]greedygreedyPythonJava
482O621. Task Scheduler[N]greedygreedyPythonJava
48345. Jump Game II[N]greedygreedyPython
484665. Non-decreasing Array[N]greedygreedyPython
485135. Candy[N]greedygreedyPython
486277. Find the Celebrity[N]greedygreedyPython
487406. Queue Reconstruction by Height[N]greedygreedyPython
488659. Split Array into Consecutive Subsequences[N]greedygreedyPython
489763. Partition Labels[N]greedygreedyPython
490926. Flip String to Monotone Increasing[N]greedygreedyPython
4911567. Maximum Length of Subarray With Positive Product[N]greedygreedyPython
4921864. Minimum Number of Swaps to Make the Binary String Alternating[N]greedygreedyPython
4932366. Minimum Replacements to Sort the Array[N]greedygreedyPython
4942870. Minimum Number of Operations to Make Array Empty[N]greedygreedyPython
495math note[O]math
496204. Count Primes[O]mathsieve of eratosthenesPython
497O50. Pow(x, n)[O]mathexponentiation by squaringPython
498470. Implement Rand10() Using Rand7()[O]mathrejection samplingPython
499O7. Reverse Integer[O]mathmathPython
500O9. Palindrome Number[O]mathmathPythonJava
501O67. Add Binary[O]mathmathPythonJava
502O338. Counting Bits[O]mathmathPythonJava
503415. Add Strings[O]mathmathPython
504263. Ugly Number[O]mathmathPython
505258. Add Digits[O]mathmathPython
506171. Excel Sheet Column Number[O]mathmathPython
507869. Reordered Power of 2[O]mathmathPython
508391. Perfect Rectangle[O]mathmathPython
509segment-tree note[P]segment-tree
510307. Range Sum Query - Mutable[P]segment-treeuse sum type segment treePython
511715. Range Module[P]segment-treeuse count type segment treePython
512729. My Calendar I[P]segment-treeuse count type segment treePython
5132158. Amount of New Area Painted Each Day[P]segment-treeuse count type segment treePython
514218. The Skyline Problem[P]segment-treeuse max type segment treePython
5152407. Longest Increasing Subsequence II[P]segment-treeuse max type segment treePython
516731. My Calendar II[P]segment-treeuse max type segment treePython
517basic note[Q]basic

How to crack LeetCode problems?

  • For beginners tackling 0-50 problems, I suggest starting with curated lists like the Blind 75, Grind 75, or NeetCode 150. It's crucial to begin with EASY problems. If you stumble upon unfamiliar concepts, don't hesitate to seek explanations from sources like ChatGPT or the LeetCode discussion section. Aim to accomplish three key objectives during this phase: familiarize yourself with the syntax of common data structures like hashmaps or stacks, gain a holistic understanding of necessary DSA topics, and grasp at least the brute force solution approach, along with basic analyses of time and space complexity for each problem.

  • For intermediates solving 50-150 problems, understanding how and why to evolve from brute force to more efficient or optimal solutions is vital. Grasping this transition process is crucial, not just memorizing solutions. At this stage, deeply comprehending the improvement mechanism is the key focus. Often, the breakpoint for a better solution comes from identifying and refining repetitive elements in the old approach.

  • After solving over 150 problems, you'll likely have covered around 85% of the topics that could appear in coding interviews. The key at this phase is to recognize patterns across the multitude of problems available. Most questions are variations of a few core concepts or classic problems you've already encountered. Identifying these patterns allows you to apply known solutions to new, seemingly complex problems effectively. Additionally, you should start using different approaches to solve the same problem you already know. For example, if it is a DFS problem, then try both recursive and iterative methods; if it is a DP problem, try both top-down and bottom-up approaches; if it is a top-k problem, then try using a heap or sort. Furthermore, attempt to understand other solutions that use approaches that are not as straightforward as your old solutions. At this stage, it's crucial to recognize recurring patterns across various problems, understanding both the application of consistent approaches to different questions and the exploration of multiple strategies for a single problem. This dual awareness enables a more adaptable and comprehensive problem-solving skill set.

How to prepare for a coding interview?

During the interview, consistently share your thoughts with the interviewer and use code comments to note down things while speaking. This ensures both of you are aligned.

  1. Clarify the requirement of the question. You must truly understand what the question is asking for. Once you understand the question’s description, make sure to generate a simple input and expect output other than the given one based on your understanding and ask the interviewer if it is correct or not.

  2. Clarify the input and output and edge cases. For instance, ask if the input array is sorted or not, whether numbers can include positives, negatives, or zeros, or if the input contains duplicate elements or not, and how to handle multiple valid answers, whether to return the first one or if outputs need sorting, and what is the expected outcome when there are no valid solutions. Take LC 209. Minimum Size Subarray Sum as example, if numbers in the input array can be negative, the question will become LC 862. Shortest Subarray with Sum at Least K, and will need a totally different approach to solve.

  3. Start from a brute force approach, just write some very simple pseudo steps of it. If you have ideas to improve it, then you can continue to explain how and where you can improve and use which DSA or classic pattern (like prefix sum, trie, or two pointers) to solve it. Mention the expected time and space complexity. If you do not have any clue to improve the naive approach, then just ask the interviewer, can we start from implementing this naive approach or not, sometimes you would find out the key point during implementing. If it is not allowed, then try to analyze some common DSA which might be useful here to the interviewer and try to get some feedback or hints from the interviewer. (If brute force is still too hard to get, then try to simulate the whole process in the description of the question or try to enumerate things).

  4. Implement the solution, adhering to proper coding style and indentation. If stuck on certain code, ask to finish the main logic first and leave comments for unimplemented parts. If stuck on the main logic, share your thoughts and discuss potential next steps with the interviewer.

  5. Final check and dry run. Once finished, do not directly run the code, because there might be some stupid typos in the code. Ask if you can dry run one time, and explain the whole process and your thoughts.

  6. Test and debug. Generate 1 or 2 standard cases and 1 or 2 edge cases which are related to the problem. And start testing and debugging. Avoid trial and error multiple times when debugging. Fix the error you find, and try to quickly dry run once and explain why you fix the error.

  7. Complexity analysis. Try to analyze the time and space complexity about your solutions and try to mention some potential follow up questions you have thought of. Like what if we change the condition of the input, or what if we have a limitation on time or space usage.

About

LeetCode Pattern 500 offers 500 solutions for LeetCode problems in Python and Java, 17 notes on essential concepts related to data structures and algorithms, and 130 patterns for solving LeetCode problems.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp