face-tongue-moneyGreedy

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 Answerarrow-up-right. TC: O(nlogn+mlogm)O(n*logn+m*logm), SC: O(logn+logm)O(logn+logm)

  • LC 376. Wiggle Subsequence

  • LC 53. Maximum Subarray

  • LC 122. Best Time to Buy and Sell Stock II

  • LC 55. Jump Game

  • LC 45. Jump Game II

    • Optimal Answerarrow-up-right. TC: O(n)O(n), SC: O(1)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

  • 🟠 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 Answerarrow-up-right. TC: O(n)O(n), SC: O(1)O(1)

  • 🟠 LC 3397. Maximum Number of Distinct Elements After Operations

    • Got stuck in some corner cases and then checked the answer directly.

    • Optimal Answerarrow-up-right. TC: O(nlogn)O(n*logn), SC: O(sorting)O(sorting)

  • LC 860. Lemonade Change

  • 🟠 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 Answerarrow-up-right. TC: O(n)O(n), SC: O(n)O(n)

  • 🟠 LC 968. Binary Tree Cameras

  • 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)O(n) time and O(1)O(1) space. The answer is genius...

    • Optimal Answerarrow-up-right. TC: O(n)O(n), SC: O(1)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 < r and 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 Answerarrow-up-right. TC: O(n)O(n), SC: O(1)O(1)

  • LC 1975. Maximum Matrix Sum

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.

      • Answerarrow-up-right. TC: O(mlogS)O(mlogS), SC: O(1)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 Answerarrow-up-right. TC: O(mlogm)O(m*logm), SC: O(logm)O(logm)

Kadane's Algorithm (2)

  • LC 53. Maximum Subarray

  • 🔴 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: Answerarrow-up-right. TC: O(n)O(n), SC: O(n)O(n)

    • 👍 Approach 2 using Kadane's Algorithm purely: Optimal Answerarrow-up-right. TC: O(n)O(n), SC: O(1)O(1)

  • LC 3147. Taking Maximum Energy From the Mystic Dungeon

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)O(1). To understand it (I finally could make it by myself on 12/14/2025!)

      • Optimal Answerarrow-up-right. TC: O(n)O(n), SC: O(1)O(1)

  • 🔴 LC 406. Queue Reconstruction by Height

  • 🟠 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 solutionarrow-up-right by myself. Although it's not as concise as the official solution in editorial, it's more understandable.

    • Optimal Solutionarrow-up-right. TC: O(n)O(n), SC: O(1)O(1)

  • LC 1717. Maximum Score From Removing Substrings

    • Key point: If you removed all ab from initial s, you will never get any ab while removing ba after that, and vice versa

      • When you removed all ab you removed ALL a that have b on the right of it.

      • Let's say we removed ba now and get ab (character a from the left of the b and character b on the right of the a, so after deleting ba strings concatenate into ab).

      • But stop, "character b on the fight of the a", but didn't I tell you one point back that we deleted ALL a that had b on right? This concludes my proof; you can prove so for ba as well, because situations are symmetrical.

    • Approach 1 using Stack: Answerarrow-up-right. TC: O(n)O(n), SC: O(n)O(n)

    • 👍 Approach 2 using 2 pointers: Optimal Answerarrow-up-right. TC: O(n)O(n), SC: O(n)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

  • LC 435. Non-overlapping Intervals

  • 🟠 LC 763. Partition Labels

  • LC 56. Merge Intervals

    • Try to do it without modifying the original input arrays during for loop!

    • Optimal Answerarrow-up-right. TC: O(nlogn)O(n*log{n}), SC: O(logn)O(log{n})

  • LC 3394. Check if Grid can be Cut into Sections

  • 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 Answerarrow-up-right. TC: O(n)O(n), SC: O(1)O(1)

  • LC 252. Meeting Rooms

  • 🟠 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 gaps array correctly: When there are N non-overlapping meetings, there will be N+1 gaps (some of which may have a duration of 0).

    • Optimal Answerarrow-up-right. TC: O(n)O(n), SC: O(n)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(nlogn)O(n*log{n}) but TreeMap can be further optimized to 2 arrays: leftMaxArray and rightMaxArray

    • Optimal Answerarrow-up-right. TC: O(n)O(n), SC: O(n)O(n)

  • LC 228. Summary Ranges

Last updated