Curated, well-documented Python 3 solutions for a growing set of LeetCode problems. Every problem folder contains:
README.mdwith a concise explanation and complexity notes.solution.pywith a clean, runnable implementation.
Use this repo as a reusable algorithm notebook or a quick refresher before interviews.
####-problem-name/— individual problems; runpython solution.pyto execute the reference implementation.0000-notes/— reusable study notes and templates grouped by topic.LICENSE— repository license details.
flowchart TD
Repo[LeetCode Solutions Repo]
Repo --> Problems["####-problem-name/"]
Repo --> Notes["0000-notes/"]
Repo --> License["LICENSE"]
Problems --> ProblemReadme["README.md (approach + complexity)"]
Problems --> ProblemSolution["solution.py (reference implementation)"]
Notes --> NotesTopics["Topic folders & guides"]
- How to think: reading prompts, choosing algorithms, and avoiding common mistakes.
- Core patterns: arrays, hashing, two pointers, sliding window, binary search, prefix sum, greedy, and dynamic programming.
- Practice helpers: quick references for frequency counting, bucket techniques, interval scheduling, and more.
- 00-how-to-think: how to read problems, how to choose algorithms, common mistakes
- 01-array: array basics
- 02-hash-table: hashing basics, frequency count, bucket technique
- 03-two-pointers: two-pointers basics
- 04-sliding-window: fixed window, variable window
- 05-binary-search: binary search on array, binary search on answer
- 06-prefix-sum: prefix sum, difference array
- 07-greedy: greedy thinking, interval greedy
- 08-dynamic-programming: introduction, state design, dp on array, dp on strings, summary
- 09-graph: graph basics, BFS, union find, topological sort, shortest path (Dijkstra)
- 10-how-to-choose-algorithm: algorithm selection guide
- 11-pattern-library: core patterns
每篇笔记都有对应的中文版本,文件名以
-zh.md结尾。
- Clone:
git clone <repo-url> - Navigate to a problem directory, e.g.
cd 0001-two-sum. - Run the solution:
python solution.py - Read the accompanying
README.mdfor the approach, complexity, and edge cases.
Tip: The folder naming convention is
<problem-id>-<kebab-case-title>so you can jump directly to the problem you want to review.
Each problem folder follows a consistent template so you can skim or reuse ideas quickly.
README.mdcontains the problem summary, key insight, approach, complexity, and a short takeaway.solution.pyis a runnable reference solution with type hints and inline notes for tricky steps.
If you want to add a new problem, start by copying any existing folder and updating the metadata.
There is no centralized test runner yet, but you can sanity-check a solution with:
python solution.py(runs the sample input if present)python -m py_compile solution.py(verifies syntax)
A topic-focused index of all solutions currently in the repository.
| ID | Problem | Topics |
|---|---|---|
| 0001 | Two Sum | Array; Hash Table |
| 0002 | Add Two Numbers | Linked List; Math |
| 0003 | Longest Substring Without Repeating Characters | Sliding Window; Hash Map |
| 0004 | Median of Two Sorted Arrays | Binary Search; Divide & Conquer |
| 0005 | Longest Palindromic Substring | String; Expand Around Center; Dynamic Programming |
| 0006 | Zigzag Conversion | String; Simulation |
| 0007 | Reverse Integer | Math; Overflow Handling |
| 0008 | String to Integer (atoi) | String; Parsing |
| 0009 | Palindrome Number | Math; Two Pointers |
| 0010 | Regular Expression Matching | Dynamic Programming; Pattern Matching |
| 0011 | Container With Most Water | Array; Two Pointers |
| 0012 | Integer to Roman | Math; Mapping |
| 0013 | Roman to Integer | Math; Mapping |
| 0026 | Remove Duplicates from Sorted Array | Array; Two Pointers |
| 0027 | Remove Element | Array; Two Pointers |
| 0028 | Find the Index of the First Occurrence in a String | String; Two Pointers |
| 0049 | Group Anagrams | Hash Table; Sorting |
| 0066 | Plus One | Array; Simulation |
| 0085 | Maximal Rectangle | Dynamic Programming; Stack; Monotonic Stack |
| 0121 | Best Time to Buy and Sell Stock | Array; Greedy |
| 0164 | Maximum Gap | Bucket Sort; Sorting |
| 0220 | Contains Duplicate III | Sliding Window; Bucket Sort |
| 0242 | Valid Anagram | Hash Table; Sorting |
| 0347 | Top K Frequent Elements | Heap; Hash Table |
| 0451 | Sort Characters by Frequency | Heap; Hash Table |
| 0679 | 24 Game | Backtracking; DFS; Math |
| 0692 | Top K Frequent Words | Heap; Sorting |
| 0712 | Minimum ASCII Delete Sum for Two Strings | Dynamic Programming; String |
| 0756 | Pyramid Transition Matrix | Graph; DFS |
| 0840 | Magic Squares in Grid | Array; Enumeration |
| 0865 | Smallest Subtree with all the Deepest Nodes | Tree; Depth-First Search; Binary Tree; Lowest Common Ancestor |
| 0912 | Sort an Array | Sorting; Divide & Conquer |
| 0944 | Delete Columns to Make Sorted | Greedy; String |
| 0955 | Delete Columns to Make Sorted II | Greedy; String |
| 0960 | Delete Columns to Make Sorted III | Dynamic Programming; String |
| 0961 | N-Repeated Element in Size 2N Array | Hash Table; Counting |
| 1161 | Maximum Level of a Binary Tree | Tree; BFS |
| 1266 | Minimum Time Visiting All Points | Geometry; Greedy; Math |
| 1339 | Maximum Product of Splitted Binary Tree | Tree; Depth-First Search; Binary Tree; Math |
| 1351 | Count Negative Numbers in a Sorted Matrix | Matrix; Binary Search |
| 1390 | Four Divisors | Math; Number Theory |
| 1411 | Number of Ways to Paint N x 3 Grid | Dynamic Programming; Combinatorics |
| 1458 | Max Dot Product of Two Subsequences | Dynamic Programming; Array; Subsequence |
| 1523 | Count Odd Numbers in an Interval Range | Math |
| 1590 | Make Sum Divisible by P | Prefix Sum; Hash Map; Modular Arithmetic |
| 1895 | Largest Magic Square | Prefix Sum; Matrix; Enumeration |
| 1925 | Count Square Sum Triples | Math; Number Theory |
| 1970 | Last Day Where You Can Still Cross | Binary Search; BFS/Union Find |
| 1975 | Maximum Matrix Sum | Greedy; Matrix |
| 2047 | Find the Largest Area of Square Inside Two Rectangles | Geometry; Brute Force; Enumeration |
| 2054 | Two Best Non-Overlapping Events | Dynamic Programming; Sorting |
| 2092 | Find All People With Secret | Graph; Union Find |
| 2110 | Number of Smooth Descent Periods of a Stock | Sliding Window; Counting |
| 2141 | Maximum Running Time of N Computers | Binary Search; Greedy |
| 2147 | Number of Ways to Divide a Long Corridor | Dynamic Programming; Combinatorics |
| 2211 | Count Collisions on a Road | Simulation; Stack |
| 2273 | Find Resultant Array After Removing Anagrams | String; Stack |
| 2343 | Query Kth Smallest Trimmed Number | Sorting; Priority Queue |
| 2402 | Meeting Rooms III | Priority Queue; Scheduling |
| 2435 | Paths in Matrix Whose Sum Is Divisible by K | Dynamic Programming; Prefix Sum |
| 2483 | Minimum Penalty for a Shop | Prefix Sum; Enumeration |
| 2872 | Maximum Number of K-Divisible Components | Tree; DFS; Greedy; Graph |
| 2943 | Maximize Area of Square Hole in Grid | Greedy; Geometry; Sorting |
| 2975 | Maximum Square Area by Removing Fences From a Field | Geometry; Hash Set; Greedy |
| 3074 | Apple Redistribution into Boxes | Greedy; Sorting |
| 3075 | Maximize Happiness of Children | Greedy; Sorting |
| 3381 | Maximum Subarray Sum With Length Divisible by K | Prefix Sum; Hash Map |
| 3432 | Count Partitions with Even Sum Difference | Prefix Sum; Math |
| 3433 | Count Mentions Per User | Simulation; Hash Table |
| 3453 | Separate Squares I | Binary Search; Geometry; Prefix Area |
| 3454 | Separate Squares II | Sweep Line; Segment Tree; Geometry; Binary Search |
| 3512 | Minimum Operations to Make Array Sum Divisible by K | Math; Greedy; Modular Arithmetic |
| 3531 | Count Covered Buildings | Geometry; Prefix Min/Max |
| 3562 | Maximum Profit from Trading Stocks with Discounts | Tree DP; Knapsack |
| 3573 | Best Time to Buy and Sell Stock V | Dynamic Programming; State Machine |
| 3577 | Count the Number of Computer Unlocking Permutations | Sorting; Feasibility Check |
| 3578 | Count Partitions With Max-Min Difference at Most K | Sliding Window; Dynamic Programming |
| 3583 | Count Special Triplets | Hash Map; Prefix/Suffix Counting |
| 3606 | Coupon Code Validator | String; Simulation |
| 3623 | Count Number of Trapezoids I | Array; Geometry |
| 3625 | Count Number of Trapezoids II | Array; Geometry |
| 3652 | Best Time to Buy and Sell Stock using Strategy | Prefix Sum; Sliding Window |
- Follow the existing folder naming pattern (
####-title-in-kebab-case). - Provide both
README.md(approach & complexity) andsolution.py(idiomatic Python). - Keep explanations concise and prefer standard library solutions unless an external dependency is necessary.
Happy coding and studying! 🚀