Greedy
Notes and typical questions for greedy algorithm
Notes
No pattern!
Typical Questions (29)
Other Greedy Algos (14)
🟠 LC 455. Assign Cookies
Must do sorting first and then use 2 pointers. Otherwise, you will get Time Limit Exceeded (even with
Map)Optimal Answer. TC: O(n∗logn+m∗logm), SC: O(logn+logm)
⚪ LC 376. Wiggle Subsequence
I made it! Be careful for the corn case: [0,0] and simplify the solution.
Optimal Answer. TC: O(n), SC: O(1)
⚪ LC 53. Maximum Subarray
Easy to miss some corner cases.
Optimal Answer. TC: O(n), SC: O(1)
This can also be resolved by DP
LC 122. Best Time to Buy and Sell Stock II
Optimal Answer. TC: O(n), SC: O(1)
This can also be resolved by DP
⚪ LC 55. Jump Game
Optimal Answer. TC: O(n), SC: O(1)
Kind of tricky in the greedy solution. Think of it in terms of coverage.
⚪ LC 45. Jump Game II
Optimal Answer. TC: O(n), SC: O(1)
If adding early quit logic, it's easy to miss a corner case (only 1 element).
Kind of tricky in the greedy solution. Think of it in terms of coverage.
⚪ LC 1005. Maximize Sum Of Array After K Negations
Easy to miss corn cases and optimal implementation...
Optimal Answer. TC: O(n∗logn), SC: O(n)
🟠 LC 134. Gas Station
Always get stuck in the middle. I can come up with an idea that we need to do
gas[i]-cost[i]but it's hard to know where is start index. Check the 代码随想录 video and explanation below为什么遍历到结尾就可以选择出答案呢,而且答案就是第一个遍历到结尾累加和不为0的位置,是否答案可能是该位置后面的位置呢;这就需要关注一个概念,净累加油量,既然第一个遍历到结尾的位置使得在后续所有位置都满足净累加油都大于0,那么为什么不从这第一个位置出发,而是从后续的位置出发呢,毕竟这第一个位置使得后续的所有位置的起始油量都大于0,如果选择后续任意一个位置,油量都需要从0开始,通过的可能肯定不如有油;总结就是:起始给你油你都不过,凭什么你起始油=0能过呢;
Optimal Answer. TC: O(n), SC: O(1)
🟠 LC 3397. Maximum Number of Distinct Elements After Operations
Got stuck in some corner cases and then checked the answer directly.
Optimal Answer. TC: O(n∗logn), SC: O(sorting)
⚪ LC 860. Lemonade Change
Easy to make stupid mistakes when my mind is not clear.
Optimal Answer. TC: O(n), SC: O(1)
🟠 LC 738. Monotone Increasing Digits
It's kind of impossible to come up with the correct idea and pass all tests without knowing the answer...I made it after a few months (recall the solution after thinking for a while)!Optimal Answer. TC: O(n), SC: O(n)
🟠 LC 968. Binary Tree Cameras
List all state combinations in a table to check.
Optimal Answer. TC: O(n), SC: O(h)
⚪ LC 605. Can Place Flowers
🔴 LC 334. Increasing Triplet Subsequence
Using the DP solution of the longest increasing subsequence will cause TLE...
It's super hard to solve it within O(n) time and O(1) space. The answer is genius...
Optimal Answer. TC: O(n), SC: O(1)
I would like to point out that for [1, 3, 0, 5] we will eventually arrive at big = 3 and small = 0 so big may come before small. However, the solution still works, because big only gets updated when there exists a small that comes before it.
🟠 LC 680. Valid Palindrome II
Missing the invariant mindset. The key invariant is:
While
l < rand chars match, you’re still on the “main path”.The first mismatch is the only decision point, and it creates two deterministic subproblems (“is substring palindrome?”).
Optimal Answer. TC: O(n), SC: O(1)
⚪ LC 1975. Maximum Matrix Sum
Optimal Answer. TC: O(n2), SC: O(1)
Resource Balancing
🟠 LC 2141. Maximum Running Time of N Computers
Kind of tricky. The hard part is not the implementation, but realizing this is a resource balancing/leveling problem rather than a simulation problem.
Approach 1 using Binary Search:
For a target running time t, each battery can contribute at most min(battery[i], t), so check whether sum(min(battery[i], t)) >= (long) n * t.
Answer. TC: O(mlogS), SC: O(1)
👍 Approach 2 using Greedy Leveling
Sort batteries. Treat the smallest m-n batteries as extra power, and the largest n batteries as the initial live batteries for n computers. Then use the extra power to level up the smaller live batteries step by step.
Optimal Answer. TC: O(m∗logm), SC: O(logm)
Kadane's Algorithm (2)
🔴 LC 918. Maximum Sum Circular Subarray
Before solving this problem, check LC 53. Maximum Subarray first!
Think of it in 2 cases.
Case 1: Normal Kadane's Algo result like LC 53 within single array.
Case 2: Result across 2 arrays.
Approach 1 using Kadane's Algorithm + prefixSum: Answer. TC: O(n), SC: O(n)
👍 Approach 2 using Kadane's Algorithm purely: Optimal Answer. TC: O(n), SC: O(1)
LC 3147. Taking Maximum Energy From the Mystic Dungeon
Optimal Answer. TC: O(n), SC: O(1)
Split-and-Solve Dual Requirements (3)
LC 135. Candy
Approach 1 (2 arrays): If you need to compare each item of a list with its 2 neighbors, handle each side separately.
🟠 Approach 2 (Optimal)
This optimal answer is super hard to understand, and can resolve this question in SC O(1). To understand it (I finally could make it by myself on 12/14/2025!)
Check this reference first.
Optimal Answer. TC: O(n), SC: O(1)
🔴 LC 406. Queue Reconstruction by Height
Optimal Answer. TC: O(n2), SC: O(n)
🟠 LC 32. Longest Valid Parentheses
Not sure if the optimal solution to this question should be put under the greedy algo topic.
This problem can also be resolved by DP and Stack approaches but SC in these 2 approaches is worse than the optimal solution.
I came up with a Stack solution by myself. Although it's not as concise as the official solution in editorial, it's more understandable.
Optimal Solution. TC: O(n), SC: O(1)
⚪ LC 1717. Maximum Score From Removing Substrings
Key point: If you removed all
abfrom initials, you will never get anyabwhile removingbaafter that, and vice versaWhen you removed all
abyou removed ALLathat havebon the right of it.Let's say we removed
banow and getab(characterafrom the left of theband characterbon the right of thea, so after deletingbastrings concatenate intoab).But stop, "character
bon the fight of thea", but didn't I tell you one point back that we deleted ALLathat hadbon right? This concludes my proof; you can prove so forbaas well, because situations are symmetrical.
Approach 1 using Stack: Answer. TC: O(n), SC: O(n)
👍 Approach 2 using 2 pointers: Optimal Answer. TC: O(n), SC: O(n)
Range/Interval Problem (10)
Notes
Be careful about the LAST interval/range during processing. It's easy to miss that!
⚪ LC 452. Minimum Number of Arrows to Burst Balloons
Try not to modify the input array.
Optimal Answer. TC: O(n∗logn), SC: O(logn)
⚪ LC 435. Non-overlapping Intervals
Optimal Answer. TC: O(n∗logn), SC: O(logn)
🟠 LC 763. Partition Labels
I made it!
Optimal Answer. TC: O(n), SC: O(1)
⚪ LC 56. Merge Intervals
Try to do it without modifying the original input arrays during
forloop!Optimal Answer. TC: O(n∗logn), SC: O(logn)
LC 3394. Check if Grid can be Cut into Sections
Optimal Answer. TC: O(n∗logn), SC: O(sorting)
⚪ LC 57. Insert Interval
It's easy to miss the clearest approach and end up in a convoluted line of thought.
Resolve it step by step based on different sections of the intervals array.
Optimal Answer. TC: O(n), SC: O(1)
LC 252. Meeting Rooms
Optimal Answer. TC: O(n∗logn), SC: O(n)
🟠 LC 3439. Reschedule Meetings for Maximum Free Time I
Although I came up with a solution after thinking about it for a long time, I made the problem too complicated. The core point is how to construct the
gapsarray correctly: When there are N non-overlapping meetings, there will be N+1 gaps (some of which may have a duration of 0).Optimal Answer. TC: O(n), SC: O(n)
⚪ LC 3440. Reschedule Meetings for Maximum Free Time II
I put this problem here because LC 3439 is listed above, but not sure if "Greedy" is the best section for this problem
I got the core idea and came up with a solution with TC O(n∗logn) but
TreeMapcan be further optimized to 2 arrays:leftMaxArrayandrightMaxArrayOptimal Answer. TC: O(n), SC: O(n)
⚪ LC 228. Summary Ranges
Optimal Answer. TC: O(n), SC: O(1)
Last updated