forked fromkamyu104/LeetCode-Solutions
- Notifications
You must be signed in to change notification settings - Fork0
🏋️ Python / Modern C++ Solutions of All 2573 LeetCode Problems (Weekly Update)
License
NotificationsYou must be signed in to change notification settings
coderpick/LeetCode-Solutions
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
- R.I.P. to my old Leetcode repository, where there were
5.7k+stars and2.2k+forks (ever the top 3 in the field). - Since free questions may be even mistakenly taken down by some companies, only solutions will be post on now.
- There are new LeetCode questions every week. I'll keep updating for full summary and better solutions.
- For more problem solutions, you can see myLintCode,GoogleKickStart,GoogleCodeJamIO repositories.
- For more challenging problem solutions, you can also see myGoogleCodeJam,MetaHackerCup repositories.
- Hope you enjoy the journey of learning data structures and algorithms.
- Notes: "🔒" means your subscription ofLeetCode premium membership is required for reading the question.
- Bit Manipulation
- Array
- String
- Linked List
- Stack
- Queue
- Binary Heap
- Tree
- Hash Table
- Math
- Sort
- Two Pointers
- Recursion
- Binary Search
- Binary Search Tree
- Breadth-First Search
- Depth-First Search
- Backtracking
- Dynamic Programming
- Greedy
- Graph
- Geometry
- Simulation
- Design
- Concurrency
- C++
- Python
- Algorithms
- Math
- Visualization
- Handy Table
- Thinking Techniques as follows:
| n | Complexity | Possible Algorithms & Techniques |
|---|---|---|
| 1018+ | O(1) | Math |
| 1018 | O(logn) | Binary & Ternary Search / Matrix Power / Cycle Tricks / Big Simulation Steps / Values Reranking / Math |
| 1016 | O(n1/2) | Math |
| 108 | O(n) | Greedy / Ad-hoc / DP |
| 4×107 | O(nlogn) | Linear # Calls to Binary & Ternary Search / Pre-processing & Querying / Divide and Conquer |
| 104 | O(n2) | Ad-hoc / DP / Greedy / Divide and Conquer / Branch and Bound |
| 500 | O(n3) | Ad-hoc / DP / Greedy / Divide and Conquer / Branch and Bound |
| 90 | O(n4) | Ad-hoc / DP / Greedy / Divide and Conquer / Branch and Bound |
| 50 | O(n5) | Branch and Bound |
| 40 | O(n×2n/2) | Meet in the Middle |
| 20 | O(n×2n) | Backtracking / Generating 2n Subsets / Bitmask Technique |
| 11 | O(n!) | Factorial / Permutation / Combination Algorithm |
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
|---|---|---|---|---|---|---|---|
| 1424 | Diagonal Traverse II | C++Python | O(m * n) | O(m) | Medium | ||
| 1438 | Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit | C++Python | O(n) | O(n) | Hard | Mono Deque | |
| 1499 | Max Value of Equation | C++Python | O(n) | O(n) | Hard | Mono Deque | |
| 1696 | Jump Game VI | C++Python | O(n) | O(k) | Medium | Mono Deque, Sliding Window | |
| 2398 | Maximum Number of Robots Within Budget | C++Python | O(n) | O(n) | Hard | Mono Deque, Sliding Window, Two Pointers |
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
|---|---|---|---|---|---|---|---|
| 1106 | Parsing A Boolean Expression | C++Python | O(n) | O(n) | Hard |
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
|---|---|---|---|---|---|---|---|
| 1373 | Maximum Sum BST in Binary Tree | C++Python | O(n) | O(h) | Hard | DFS, Stack | |
| 1382 | Balance a Binary Search Tree | C++Python | O(n) | O(h) | Medium | DFS, Stack | |
| 1902 | Depth of BST Given Insertion Order | C++Python | O(nlogn) | O(n) | Medium | 🔒 | Sorted Dict |
| 1932 | Merge BSTs to Create Single BST | C++Python | O(n) | O(n) | Hard | BST, BFS | |
| 2426 | Number of Pairs Satisfying Inequality | C++Python | O(nlogn) | O(n) | Hard | Merge Sort, Two Pointers, BIT, Fenwick Tree, Coordinate Compression, Sorted List, Ordered Set, Binary Search |
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
|---|---|---|---|---|---|---|---|
| 1042 | Flower Planting With No Adjacent | C++Python | O(n) | O(n) | Easy | ||
| 1101 | The Earliest Moment When Everyone Become Friends | C++Python | O(nlogn) | O(n) | Medium | 🔒 | Union Find |
| 1135 | Connecting Cities With Minimum Cost | C++Python | O(nlogn) | O(n) | Medium | 🔒 | Union Find,Kruskal's Algorithm, MST |
| 1168 | Optimize Water Distribution in a Village | C++Python | O(nlogn) | O(n) | Hard | 🔒 | Union Find |
| 1334 | Find the City With the Smallest Number of Neighbors at a Threshold Distance | C++Python | O(n^3) | O(n^2) | Medium | Floyd-Warshall Algorithm | |
| 1349 | Maximum Students Taking Exam | C++Python | O(m * n * sqrt(m * n)) | O(m + n) | Hard | GCJ2008 - Round 3 | Hopcroft-Karp Bipartite Matching,Hungarian Bipartite Matching, Maximum Independent Set |
| 1361 | Validate Binary Tree Nodes | C++Python | O(n) | O(n) | Medium | DFS, Tree | |
| 1462 | Course Schedule IV | C++Python | O(n^3) | O(n^2) | Medium | Floyd-Warshall Algorithm | |
| 1489 | Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree | C++Python | O(nlogn) | O(n) | Hard | Kruskal Algorithm | |
| 1557 | Minimum Number of Vertices to Reach All Nodes | C++Python | O(e) | O(n) | Medium | ||
| 1579 | Remove Max Number of Edges to Keep Graph Fully Traversable | C++Python | O(n + m) | O(n) | Hard | Union Find | |
| 1584 | Min Cost to Connect All Points | C++Python | O(n^2) | O(n) | Medium | Union Find,Kruskal's Algorithm, MST | |
| 1601 | Maximum Number of Achievable Transfer Requests | C++Python | O((n + r) * 2^r) | O(n + r) | Hard | Combinations, Backtracking | |
| 1615 | Maximal Network Rank | C++Python | O(m + n + k^2) | O(m + n) | Medium | Counting Sort | |
| 1627 | Graph Connectivity With Threshold | C++Python | O(nlogn + q) | O(n) | Hard | Union Find, Math | |
| 1631 | Path With Minimum Effort | C++Python | O(m * n * log(m * n)) | O(m * n) | Medium | Binary Search, DFS, BFS, Bi-BFS, Union Find,Dijkstra's Algorithm | |
| 1697 | Checking Existence of Edge Length Limited Paths | C++Python | O(nlogn + mlogm) | O(n) | Hard | Union Find | |
| 1719 | Number Of Ways To Reconstruct A Tree | C++Python | O(nlogn) | O(n) | Hard | ||
| 1724 | Checking Existence of Edge Length Limited Paths II | C++Python | ctor:O(nlogn + mlogm) query:O(logn) | O(nlogn + m) | Hard | 🔒 | Versioned Union Find, Binary Lifting |
| 1743 | Restore the Array From Adjacent Pairs | C++Python | O(n) | O(n) | Medium | ||
| 1761 | Minimum Degree of a Connected Trio in a Graph | C++Python | O(n^3) | O(n^2) | Hard | ||
| 1778 | Shortest Path in a Hidden Grid | C++Python | O(m * n) | O(m * n) | Medium | 🔒 | DFS, BFS, Bi-BFS |
| 1782 | Count Pairs Of Nodes | C++Python | O(n + e + q) | O(n + e) | Hard | Counting, Two Pointers | |
| 1786 | Number of Restricted Paths From First to Last Node | C++Python | O(|E| * log|V|) | O(|E|) | Medium | Dijkstra's Algorithm, DP | |
| 1791 | Find Center of Star Graph | C++Python | O(n) | O(n) | Medium | ||
| 1810 | Minimum Path Cost in a Hidden Grid | C++Python | O(m * n * log(m * n)) | O(m * n) | Medium | 🔒 | DFS,Dijkstra's Algorithm |
| 1820 | Maximum Number of Accepted Invitations | C++Python | O(m * n * sqrt(m + n)) | O(m + n) | Medium | 🔒 | Hopcroft-Karp Bipartite Matching,Hungarian Bipartite Matching |
| 1879 | Minimum XOR Sum of Two Arrays | C++Python | O(n^3) | O(n^2) | Hard | DP,Hungarian Weighted Bipartite Matching | |
| 1947 | Maximum Compatibility Score Sum | C++Python | O(m^2 * (n + m)) | O(m^2) | Medium | variant ofMinimum XOR Sum of Two Arrays | DP,Hungarian Weighted Bipartite Matching |
| 1971 | Find if Path Exists in Graph | C++Python | O(|V| + |E|) | O(|V| + |E|) | Easy | DFS, BFS, Bi-BFS | |
| 1976 | Number of Ways to Arrive at Destination | C++Python | O(|E| * log|V|) | O(|E|) | Medium | Dijkstra's Algorithm | |
| 2076 | Process Restricted Friend Requests | C++Python | O(n * r) | O(n) | Hard | Union Find | |
| 2077 | Paths in Maze That Lead to Same Room | C++Python | O(|V|^3) | O(|E|) | Medium | 🔒 | |
| 2092 | Find All People With Secret | C++Python | O(nlogn) | O(nlogn) | Hard | BFS, DFS, Union Find | |
| 2093 | Minimum Path Cost in a Hidden Grid | C++Python | O(|E| * log|V|) | O(|V| + |E|) | Medium | variant ofCheapest Flights Within K Stops, 🔒 | Dijkstra's Algorithm, DP |
| 2097 | Valid Arrangement of Pairs | C++Python | O(|V| + |E|) | O(|V| + |E|) | Hard | variant ofReconstruct Itinerary | Hierholzer's Algorithm, Eulerian Path |
| 2123 | Minimum Operations to Remove Adjacent Ones in Matrix | C++Python | O(m * n * sqrt(m * n)) | O(m + n) | Hard | variant ofMaximum Students Taking Exam, 🔒 | Hopcroft-Karp Bipartite Matching, Maximum Independent Set |
| 2127 | Maximum Employees to Be Invited to a Meeting | C++Python | O(n) | O(n) | Hard | ||
| 2172 | Maximum AND Sum of Array | C++Python | O(n^3) | O(n^2) | Hard | variant ofMaximum Compatibility Score Sum | DP,Hungarian Weighted Bipartite Matching |
| 2203 | Minimum Weighted Subgraph With the Required Paths | C++Python | O(|E| * log|V|) | O(|E|) | Hard | Dijkstra's Algorithm | |
| 2204 | Distance to a Cycle in Undirected Graph | C++Python | O(|V| + |E|) | O(|V| + |E|) | Hard | 🔒 | Graph, DFS, BFS |
| 2242 | Maximum Score of a Node Sequence | C++Python | O(|V| + |E|) | O(|V|) | Hard | Graph | |
| 2307 | Check for Contradictions in Equations | C++Python | O(e + q) | O(n) | Hard | 🔒, variant ofEvaluate Division | DFS, Union Find |
| 2359 | Find Closest Node to Given Two Nodes | C++Python | O(n) | O(n) | Medium | Graph, Hash Table, DFS | |
| 2360 | Longest Cycle in a Graph | C++Python | O(n) | O(n) | Hard | Graph, Hash Table, DFS | |
| 2392 | Build a Matrix With Conditions | C++Python | O(k^2 + r + c) | O(k + r + c) | Hard | Graph, Topological Sort | |
| 2473 | Minimum Cost to Buy Apples | C++Python | O(n * rlogn) | O(n) | Medium | 🔒 | Dijkstra's Algorithm |
| 2508 | Add Edges to Make Degrees of All Nodes Even | C++Python | O(n) | O(n) | Hard | Graph |
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
|---|---|---|---|---|---|---|---|
| 1453 | Maximum Number of Darts Inside of a Circular Dartboard | C++Python | O(n^2 * logn) | O(n) | Hard | Line Sweep | |
| 1515 | Best Position for a Service Centre | C++Python | O(n * iter) | O(n) | Hard | Geometric Median, Gradient Descent, Weiszfeld's Algorithm | |
| 1610 | Maximum Number of Visible Points | C++Python | O(nlogn) | O(n) | Hard | Two Pointers, Sliding Window | |
| 1924 | Erect the Fence II | C++Python | O(n) on average | O(n) | Hard | 🔒 | Welzl's Algorithm |
| 1956 | Minimum Time For K Virus Variants to Spread | C++Python | O(nlogn * logr) | O(n) | Hard | 🔒 | Geometry, Binary Search, Line Sweep, Segment Tree, Coordinate Compression |
| 2101 | Detonate the Maximum Bombs | C++Python | O(|V|^2 + \V| * |E|) | O(\V| + |E|) | Medium | Graph, DFS, BFS |
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
|---|---|---|---|---|---|---|---|
| 1138 | Alphabet Board Path | C++Python | O(n) | O(1) | Medium | ||
| 1243 | Array Transformation | C++Python | O(n^2) | O(n) | Easy | ||
| 2061 | Number of Spaces Cleaning Robot Cleaned | C++Python | O(m * n) | O(1) | Medium | 🔒 | |
| 2162 | Minimum Cost to Set Cooking Time | C++Python | O(1) | O(1) | Medium | ||
| 2257 | Count Unguarded Cells in the Grid | C++Python | O(m * n) | O(m * n) | Medium | Array, Simulation | |
| 2303 | Calculate Amount Paid in Taxes | C++Python | O(n) | O(1) | Easy | Simulation | |
| 2507 | Smallest Value After Replacing With Sum of Prime Factors | C++Python | O(s * logn) | O(max_n^0.5) | Medium | Number Theory, Simulation | |
| 2532 | Time to Cross a Bridge | C++Python | O(k + nlogk) | O(k) | Hard | Heap, Simulation | |
| 2534 | Time Taken to Cross the Door | C++Python | O(n) | O(n) | Hard | 🔒 | Queue, Simulation |
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
|---|---|---|---|---|---|---|---|
| 1146 | Snapshot Array | C++Python | set:O(1) get:O(logn) | O(n) | Medium | ||
| 1166 | Design File System | C++Python | create:O(n) get:O(n) | O(n) | Medium | 🔒 | |
| 1172 | Dinner Plate Stacks | C++Python | push:O(logn) pop:O(1), amortized popAtStack:(logn) | O(n * c) | Hard | ||
| 1206 | Design Skiplist | C++Python | O(logn), on average | O(n) | Hard | ||
| 1236 | Web Crawler | C++Python | O(|V| + |E|) | O(|V|) | Medium | 🔒 | BFS, DFS |
| 1244 | Design A Leaderboard | C++Python | ctor:O(1) add:O(1) top:O(n) reset:O(1) | O(n) | Medium | ||
| 1268 | Search Suggestions System | C++Python | ctor:O(n * l) suggest:O(l^2) | O(t) | Medium | Trie | |
| 1286 | Iterator for Combination | C++Python | O(k) | O(k) | Medium | Stack | |
| 1348 | Tweet Counts Per Frequency | C++Python | add:O(logn) query:O(c) | O(n) | Medium | ||
| 1352 | Product of the Last K Numbers | C++Python | ctor:O(1) add:O(1) get:O(1) | O(n) | Medium | ||
| 1357 | Apply Discount Every n Orders | C++Python | ctor:O(m) getBill:O(p) | O(m) | Medium | ||
| 1381 | Design a Stack With Increment Operation | C++Python | ctor:O(1) push:O(1) pop:O(1) increment:O(1) | O(n) | Medium | ||
| 1396 | Design Underground System | C++Python | ctor:O(1) checkin:O(1) checkout:O(1) getaverage:O(1) | O(n) | Medium | ||
| 1429 | First Unique Number | C++Python | ctor:O(k) add:O(1) showFirstUnique:O(1) | O(n) | Medium | 🔒 | LinkedHashSet |
| 1472 | Design Browser History | C++Python | ctor:O(1) visit:O(1) back:O(1) forward:O(1) | O(n) | Medium | ||
| 1476 | Subrectangle Queries | C++Python | ctor:O(1) update:O(1) get:O(u) | O(u) | Medium | ||
| 1483 | Kth Ancestor of a Tree Node | C++Python | ctor:O(n * logh) get:O(logh) | O(n * logh) | Hard | DP, Binary Lifting | |
| 1500 | Design a File Sharing System | C++Python | ctor:O(1) join:O(logu + c) leave:O(logu + c) request:O(u) | O(u * c) | Medium | 🔒 | |
| 1570 | Dot Product of Two Sparse Vectors | C++Python | ctor:O(n) dot_product:O(min(n, m)) | O(n) | Medium | 🔒 | |
| 1586 | Binary Search Tree Iterator II | C++Python | O(1), amortized | O(h) | Medium | 🔒 | |
| 1600 | Throne Inheritance | C++Python | ctor:O(1) birth:O(1) death:O(1) inherit:O(n) | O(n) | Medium | ||
| 1603 | Design Parking System | C++Python | O(1) | O(1) | Easy | ||
| 1622 | Fancy Sequence | C++Python | O(1) | O(n) | Hard | Euler's Theorem | |
| 1628 | Design an Expression Tree With Evaluate Function | C++Python | O(n) | O(h) | Medium | 🔒 | |
| 1656 | Design an Ordered Stream | C++Python | O(1), amortized | O(n) | Easy | ||
| 1670 | Design Front Middle Back Queue | C++Python | O(1) | O(n) | Medium | ||
| 1756 | Design Most Recently Used Queue | C++Python | ctor:O(nlogn) fetch:O(logn) | O(n) | Medium | 🔒 | Sorted List, BIT, Fenwick Tree, Square Root Decomposition |
| 1797 | Design Authentication Manager | C++Python | ctor:O(1) generate:O(1), amortized renew:O(1), amortized count:O(1), amortized | O(n) | Medium | OrderedDict | |
| 1804 | Implement Trie II (Prefix Tree) | C++Python | ctor:O(1) insert:O(n) count_word:O(n) count_prefix:O(n) erase:O(n) | O(t) | Medium | 🔒 | Trie |
| 1825 | Finding MK Average | C++Python | ctor:O(1) add_element:O(logn) calc_mkaverge:O(1) | O(m) | Hard | Sorted List | |
| 1845 | Seat Reservation Manager | C++Python | ctor:O(n) reserve:O(logn) unreserve:O(logn) | O(n) | Medium | Heap | |
| 1865 | Finding Pairs With a Certain Sum | C++Python | ctor:O(n1 + n2) add:O(1) count:O(n1) | O(n1 + n2) | Medium | Hash Table | |
| 1912 | Design Movie Rental System | C++Python | ctor:O(nlogn) search:O(logn) rent:O(logn) drop:O(logn) report:O(logn) | O(n) | Hard | Ordered List | |
| 1993 | Operations on Tree | C++Python | ctor:O(n) lock:O(1) unlock:O(1) upgrade:O(n) | O(n) | Medium | ||
| 2013 | Detect Squares | C++Python | ctor:O(1) add:O(1) count:O(n) | O(n) | Medium | ||
| 2034 | Stock Price Fluctuation | C++Python | ctor:O(1) update:O(logn) current:O(1) max:O(1) min:O(1) | O(n) | Medium | Sorted List, Heap | |
| 2043 | Simple Bank System | C++Python | ctor:O(1) transer:O(1) deposit:O(1) withdraw:O(1) | O(1) | Medium | ||
| 2069 | Walking Robot Simulation II | C++Python | O(1) | O(1) | Medium | Simulation, Math | |
| 2080 | Range Frequency Queries | C++Python | ctor:O(n) query:O(logn) | O(n) | Medium | Binary Search | |
| 2102 | Sequentially Ordinal Rank Tracker | C++Python | add:O(logn) get:O(logn) | O(n) | Hard | Sorted List | |
| 2166 | Design Bitset | C++Python | ctor:O(n) fix:O(1) fix:O(1) unfix:O(1) flip:O(1) all:O(1) one:O(1) count:O(1) toString:O(n) | O(n) | Medium | ||
| 2227 | Encrypt and Decrypt Strings | C++Python | ctor:O(m + d) encrypt:O(n) decrypt:O(n) | O(n) | Hard | Freq Table | |
| 2241 | Design an ATM Machine | C++Python | ctor:O(1) deposit:O(1) withdraw:O(1) | O(1) | Medium | Greedy | |
| 2254 | Design Video Sharing Platform | C++Python | ctor:O(1) upload:O(logn + l) remove:O(logn) like:O(1) dislike:O(1) view:O(1) getLikesAndDislikes:O(1) getViews:O(1) | O(n * l) | Hard | 🔒 | Heap |
| 2276 | Count Integers in Intervals | C++Python | ctor:O(1) add:O(logn), amortized Count:O(1) | O(n) | Hard | Sorted List | |
| 2286 | Booking Concert Tickets in Groups | C++Python | ctor:O(n) gather:O(logn) scatter:O(logn), amortized | O(n) | Hard | Segment Tree, Binary Search | |
| 2296 | Design a Text Editor | C++Python | ctor:O(1) addText:O(l) deleteText:O(k) cursorLeft:O(k) cursorRight:O(k) | O(n) | Hard | Stack | |
| 2336 | Smallest Number in Infinite Set | C++Python | ctor:O(1) popSmallest:O(logn) addBack:O(logn) | O(n) | Medium | Heap, BST | |
| 2349 | Design a Number Container System | C++Python | ctor:O(1) change:O(logn) find:O(1) | O(n) | Medium | Sorted List, BST | |
| 2353 | Design a Food Rating System | C++Python | ctor:O(nlogn) changeRating:O(logn) highestRated:O(1) | O(n) | Medium | Sorted List, BST | |
| 2408 | Design SQL | C++Python | ctor:O(t * max_m) insertRow:O(m) deleteRow:O(1) selectCell:O(m) | O(d) | Medium | 🔒 | Hash Table |
| 2424 | Longest Uploaded Prefix | C++Python | ctor:O(1) upload:O(1), amortized longest:O(1) | O(n) | Medium | Hash Table | |
| 2502 | Design Memory Allocator | C++Python | ctor:O(1) allocate:O(logn) free:O(logn) | O(n) | Medium | Sorted List | |
| 2526 | Find Consecutive Integers from a Data Stream | C++Python | O(1) | O(1) | Medium | Array |
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
|---|---|---|---|---|---|---|---|
| 1114 | Print in Order | C++Python | O(n) | O(1) | Easy | ||
| 1115 | Print FooBar Alternately | C++Python | O(n) | O(1) | Medium | ||
| 1116 | Print Zero Even Odd | C++Python | O(n) | O(1) | Medium | ||
| 1117 | Building H2O | C++Python | O(n) | O(1) | Hard | ||
| 1188 | Design Bounded Blocking Queue | C++Python | O(n) | O(1) | Medium | 🔒 | |
| 1195 | Fizz Buzz Multithreaded | C++Python | O(n) | O(1) | Medium | ||
| 1226 | The Dining Philosophers | C++Python | O(n) | O(1) | Medium | ||
| 1242 | Web Crawler Multithreaded | C++Python | O(|V| + |E|) | O(|V|) | Medium | 🔒 | |
| 1279 | Traffic Light Controlled Intersection | C++Python | O(n) | O(1) | Easy | 🔒 |
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
|---|
About
🏋️ Python / Modern C++ Solutions of All 2573 LeetCode Problems (Weekly Update)
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Packages0
No packages published
Languages
- C++54.3%
- Python45.5%
- Java0.2%
- Go0.0%
- Shell0.0%
- C#0.0%