Pattern this teaches:Greedy Scheduling and Sorting
Partition Labels
A medium Greedy interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Greedy
- Sheets
- 2
- Core for
- 3 roles
The problem
Cut a string into as many consecutive pieces as you can, with the rule that each letter lives in only one piece. Return the lengths of the pieces from left to right.
Example 1
- Input
- s = "ababcbacadefegdehijhklij"
- Output
- [9,7,8]
- Why
- Partition is "ababcbaca", "defegde", "hijhklij". Each letter appears in only one part.
Example 2
- Input
- s = "eccbbbbdec"
- Output
- [10]
- Why
- All characters appear in a single partition.
Constraints
- 1 <= s.length <= 500
- s consists of lowercase English letters
How to think about it
Updated 2026-09-29The first time a character appears, its final appearance sets the minimum ending boundary for the current segment. As you scan forward, any newly encountered character can only push this boundary further right, never shrink it.
Approaches, worst first
Interval merge
time O(n log k) · space O(k)
Record the first and last occurrence for every distinct character to form intervals, then merge overlapping intervals. Correct, but constructing and sorting intervals involves unnecessary overhead.
Greedy boundary expansionWrite this one
time O(n) · space O(1)
Precompute the last occurrence index of each character in a 26-element array. Iterate through the string, stretching the partition boundary to max(boundary, last[c]). When current index reaches boundary, seal the chunk and begin a new one.
Where people lose marks · 2
- Off-by-one in partition size: the length of a segment from start to end inclusive is `end - start + 1`, not `end - start`.
- Forgetting to update the start pointer to `i + 1` after closing a partition causes subsequent lengths to accumulate previous slices.
The theory behind it
Greedy — the ground this problem stands on. All Greedy problems
What Greedy is
A greedy algorithm makes the best-looking choice available right now, at every step, without ever looking back or second-guessing its decision. Think of a cashier making change by handing over the largest possible coin first, repeatedly, until the total is reached. Unlike dynamic programming, which saves and compares answers to multiple overlapping paths, a greedy strategy commits to one immediate option and keeps moving forward.
When to reach for it
Reach for greedy when problems ask for minimum jumps, interval scheduling, assigning resources to maximize satisfaction, or finding fractional values. Key signals include sorted orders where greedily taking the next item never hurts future options, or gas station round trips where running balances prove reachability. If you can prove that taking the immediate best choice never leaves you worse off than any alternative, greedy gives the fastest answer.
How the pattern works
Start by sorting the input to bring the most promising candidates to the front. At each position, evaluate your local rule, take the best available piece, and update your running state. The crucial mental step is proving the greedy choice property: demonstrate that picking this immediate winner cannot block a better global solution down the road. If choosing an item now forces you to reconsider past decisions when conditions change later, greedy fails and you must switch to dynamic programming instead.
More Greedy problems
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .