Collection of my solutions and analysis for problems on leetcode
| # | Title | Solution | Time | Space | Tag | One-Liner |
|---|---|---|---|---|---|---|
| 23 | Merge k Sorted Lists | C++ | O() | O() | N/A | |
| 32 | Longest Valid Parentheses | C++ | O() | O() | N/A | |
| 37 | Sudoku Solver | C++ | O() | O() | N/A | |
| 41 | First Missing Positive | C++ | O() | O() | N/A | |
| 42 | Trapping Rain Water | C++ | O() | O() | N/A | |
| 51 | N-Queens | C++ | O() | O() | N/A | |
| 52 | N-Queens II | C++ | O() | O() | N/A | |
| 72 | Edit Distance | C++ | O(n^2) | O(n^2) | DP | Plain implementation of Edit Distance! |
| 76 | Minimum Window Substring | C++ | O() | O() | N/A | |
| 84 | Largest Rectangle in Histogram | C++ | O(n) | O(n) | Monotonic Stack | |
| 85 | Maximal Rectangle | C++ | O(n^2) | O(n) | Monotonic Stack | Can we convert the problem to "Largest Rectangle in Histogram"? |
| 124 | Binary Tree Maximum Path Sum | C++ | O() | O() | N/A | |
| 126 | Word Ladder II | C++ | O() | O() | N/A | |
| 127 | Word Ladder | C++ | O() | O() | N/A | |
| 140 | Word Break II | C++ | O() | O() | N/A | |
| 149 | Max Points on a Line | C++ | O(n^2 * log(n)) | O(n) | Geometry | If you fix one point of a line, it is quite simple to find the rest of the points on the same line, right? |
| 154 | Find Minimum in Rotated Sorted Array II | C++ | O() | O() | N/A | |
| 174 | Dungeon Game | C++ | O() | O() | N/A | |
| 212 | Word Search II | C++ | O() | O() | N/A | |
| 224 | Basic Calculator | C++ | O() | O() | N/A | |
| 295 | Find Median from Data Stream | C++ | O() | O() | Priority Queue | Can we partition the input stream into 2 parts each contains the elements in sorted order? |
| 296 | Best Meeting Point | C++ | O() | O() | Math | If there are two homes, where will you set the meeting point? |
| 315 | Count of Smaller Numbers After Self | C++ | O() | O() | Segment Tree, Ordered Set | If you like to go with segment-tree, on which array you want to make the range queries? |
| 329 | Longest Increasing Path in a Matrix | C++ | O() | O() | N/A | |
| 332 | Reconstruct Itinerary | C++ | O() | O() | N/A | |
| 352 | Data Stream as Disjoint Intervals | C++ | O() | O() | N/A | |
| 363 | Max Sum of Rectangle No Larger Than K | C++ | O(n^3 * log(n)) | O(n) | Prefix Sum + Binary Search | The n^4 solution is straightforward, how can we convert it to O(n^3 * log(n))? |
| 381 | Insert Delete GetRandom O(1) - Duplicates allowed | C++ | O() | O() | N/A | |
| 675 | Cut Off Trees for Golf Event | C++ | O() | O() | BFS | Just make sure you read the line "1 represents an empty cell that can be walked through." carefully! |
| 691 | Stickers to Spell Word | C++ | O() | O() | BFS | Instead of building the target, can we walk reversely? |
| 765 | Couples Holding Hands | C++ | O() | O() | N/A | |
| 773 | Sliding Puzzle | C++ | O() | O() | N/A | |
| 827 | Making A Large Island | C++ | O() | O() | N/A | |
| 834 | Sum of Distances in Tree | C++ | O() | O() | N/A | |
| 839 | Similar String Groups | C++ | O() | O() | N/A | |
| 850 | Rectangle Area II | C++ | O() | O() | N/A | |
| 882 | Reachable Nodes In Subdivided Graph | C++ | O() | O() | N/A | |
| 895 | Maximum Frequency Stack | C++ | O() | O() | N/A | |
| 924 | Minimize Malware Spread | C++ | O() | O() | N/A | |
| 936 | Stamping The Sequence | C++ | O(n^2) | O(n) | Greedy | Can we do the conversion from target to s? |
| 952 | Largest Component Size by Common Factor | C++ | O() | O() | Union Find | |
| 968 | Binary Tree Cameras | C++ | O() | O() | Greedy/DP | When a node must have covered by a camera? |
| 980 | Unique Paths III | C++ | O() | O() | N/A | |
| 987 | Vertical Order Traversal of a Binary Tree | C++ | O() | O() | N/A | |
| 992 | Subarrays with K Different Integers | C++ | O() | O() | N/A | |
| 1032 | Stream of Characters | C++ | O() | O() | N/A | |
| 1044 | Longest Duplicate Substring | C++ | O(n * log(n)) | O(??) | Rolling Hash/String Matching | If we know a size of duplicate string, how can we confirm that? |
| 1074 | Number of Submatrices That Sum to Target | C++ | O(n^3) | O(n) | Prefix Sum | Can we convert it to a "1-d sub-array sum equal to k" problem? |
| 1095 | Find in Mountain Array | C++ | O(log(n)) | O(n) | Ternary search | I think the definition of "Mountain Array" is not correct. There will be one peak point in the mountain array. |
| 1192 | Critical Connections in a Network | C++ | O() | O() | N/A | |
| 1203 | Sort Items by Groups Respecting Dependencies | C++ | O() | O() | N/A | |
| 1220 | Count Vowels Permutation | C++ | O() | O() | DP/Memorization | The problem is pretty straight forward except thinking whether we need to store all the states. |
| 1223 | Dice Roll Simulation | C++ | O() | O() | N/A | |
| 1235 | Maximum Profit in Job Scheduling | C++ | O() | O() | DP | |
| 1289 | Minimum Falling Path Sum II | C++ | O() | O() | DP | DP states are current row and column used in previous row. |
| 1293 | Shortest Path in a Grid with Obstacles Elimination | C++ | O() | O() | N/A | |
| 1345 | Jump Game IV | C++ | O(n) | O(n) | BFS/Bidirectional BFS | Can you omit the recurring insertion of nodes in the queue? |
| 1458 | Max Dot Product of Two Subsequences | C++ | O() | O() | DP | |
| 1463 | Cherry Pickup II | C++ | O() | O() | N/A | |
| 1473 | Paint House III | C++ | O() | O() | N/A | |
| 1483 | Kth Ancestor of a Tree Node | C++ | O() | O() | N/A | |
| 1489 | Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree | C++ | O() | O() | N/A | |
| 1494 | Parallel Courses II | C++ | O() | O() | N/A | |
| 1510 | Stone Game IV | C++ | O() | O() | N/A | |
| 1526 | Minimum Number of Increments on Subarrays to Form a Target Array | C++ | O() | O() | N/A | |
| 1537 | Get the Maximum Score | C++ | O() | O() | N/A | |
| 1553 | Minimum Number of Days to Eat N Oranges | C++ | O() | O() | N/A | |
| 1575 | Count All Possible Routes | C++ | O() | O() | DP | |
| 1579 | Remove Max Number of Edges to Keep Graph Fully Traversable | C++ | O() | O() | N/A | |
| 1606 | Find Servers That Handled Most Number of Requests | C++ | O() | O() | Scheduling/Greedy | Solve problem-2402 with this. |
| 1617 | Count Subtrees With Max Distance Between Cities | C++ | O() | O() | N/A | |
| 1627 | Graph Connectivity With Threshold | C++ | O() | O() | N/A | |
| 1649 | Create Sorted Array through Instructions | C++ | O() | O() | BIT | |
| 1655 | Distribute Repeating Integers | C++ | O() | O() | Backtracking | Easy-peasy backtracking problem, nah? |
| 1675 | Minimize Deviation in Array | C++ | O() | O() | N/A | |
| 1697 | Checking Existence of Edge Length Limited Paths | C++ | O() | O() | N/A | |
| 1723 | Find Minimum Time to Finish All Jobs | C++ | O() | O() | N/A | |
| 1793 | Maximum Score of a Good Subarray | C++ | O(n) | O(n) | Monotonic Stack | Can we convert the problem to "Largest Rectangle in Histogram"? |
| 1944 | Number of Visible People in a Queue | C++ | O(n) | O(n) | Monotonic Stack | Now You See Me! |
| 2050 | Parallel Courses III | C++ | O() | O() | Topological Sorting | |
| 2076 | Process Restricted Friend Requests | C++ | O() | O() | Union Find | |
| 2092 | Find All People With Secret | C++ | O() | O() | Union Find | Just remember that a person may receive the secret and share it with people in other meetings within the same time frame, can you deal with that? |
| 2102 | Sequentially Ordinal Rank Tracker | C++ | O() | O() | Set | How can we track the current i-th best location within a sorted set? |
| 2122 | Recover the Original Array | C++ | O() | O() | Adhoc | Can you find out the smallest element of the lower array? |
| 2141 | Maximum Running Time of N Computers | C++ | O(n * log(n)) | O(1) | Binary Search | |
| 2147 | Number of Ways to Divide a Long Corridor | C++ | O(n) | O(n) | Ad-hoc | Calculate gaps in every two-seat segments |
| 2151 | Maximum Good People Based on Statements | C++ | O(2^n) | O(n) | Graph Traversal | If we know an assignment of goodness/badness of each vertices, can we validate that? |
| 2251 | Number of Flowers in Full Bloom | C++ | O(n * log(n))) | O(1) | Greedy | |
| 2301 | Match Substring After Replacement | C++ | O(n*m) | O(const.) | String Matching | Mapping keys are not unique. |
| 2302 | Count Subarrays With Score Less Than K | C++ | O(n) | O(const.) | Sliding Window | Sub-array sum with limit, what else it could be other than "sliding window"? |
| 2306 | Naming a Company | C++ | N/A | N/A | N/A | N/A |
| 2312 | Selling Pieces of Wood | C++ | N/A | N/A | DP | N/A |
| 2318 | Number of Distinct Roll Sequences | C++ | N/A | N/A | DP | N/A |
| 2328 | Number of Increasing Paths in a Grid | C++ | N/A | N/A | DP | Think about if you start your traversal from each of the array positions, how you can calculate the strictly increasing paths? |
| 2354 | Number of Excellent Pairs | C++ | N/A | N/A | Two-pointers | Can you make relation between num_bit(A & B) + num_bit(A | B) with num_bit(A) and num_bit(B)? |
| 2360 | Longest Cycle in a Graph | C++ | N/A | N/A | Graph | Finding the longest cycle in a directed or undirected graph is a NP-complete problem. Still this problem asked to find it for graphs with 10^5 nodes, how? |
| 2392 | Build a Matrix With Conditions | C++ | N/A | N/A | Topo-sort | Find topo order in row and column-wise and then boom! |
| 2402 | Meeting Rooms III | C++ | N/A | N/A | Scheduling/Greedy | You just need to remember that, "each meeting will take place in the unused room with the lowest number"! |
| 2412 | Minimum Money Required Before Transactions | C++ | N/A | N/A | Greedy | How can we generate the permutation that will require the minimum possible money? |
| 2416 | Sum of Prefix Scores of Strings | C++ | N/A | N/A | Trie | Plain and simple Trie, why you need hint then? ;) |
| 2444 | Count Subarrays With Fixed Bounds | C++ | O(n) | O(1) | Sliding Window | Just make sure you re-start the window when one of the fixed-bound subarray conditions! |
| 2448 | Minimum Cost to Make Array Equal | C++ | N/A | N/A | Binary Search on Convex Function | I am still not sure why the function of this problem is a convex one. But if you able to figure it out, rest of it is straight forward! |
| 2449 | Minimum Number of Operations to Make Arrays Similar | C++ | N/A | N/A | Greedy | Think about what you will do if you were allowed to perform +/- 1? |