This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 4: Optimisation
Take the locally best move, but only once you can argue it never hurts.
Builds on Intervals.
The question to ask
Is there a choice I can make now that provably never makes the final answer worse?
A greedy solution commits to the best-looking choice at each step and never revisits it. It's often a single pass, which makes it tempting and dangerous: many problems that look greedy need DP. The discipline is the exchange argument. If some optimal answer made a different choice, could you swap in yours without making it worse? If yes, greedy is safe. Common shapes: track the farthest reachable point, sort and match smallest with smallest, reset when a running total goes negative.
Time O(n), or O(n log n) when you sort first · Space O(1) to O(n)
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
Gas Station: if the total gas is at least the total cost, some start works. Whenever the tank drops below zero, no station from the current start up to here can be the answer, so start after it.
function canCompleteCircuit(gas: number[], cost: number[]): number {
let total = 0;
let tank = 0;
let start = 0;
for (let i = 0; i < gas.length; i++) {
const balance = gas[i] - cost[i];
total += balance;
tank += balance;
if (tank < 0) {
start = i + 1;
tank = 0;
}
}
return total < 0 ? -1 : start;
}Partition Labels: record where each letter last appears. Walk the string stretching the current part's end to cover every letter seen; when the index reaches that end, close the part.
function partitionLabels(s: string): number[] {
const last = new Map<string, number>();
for (let i = 0; i < s.length; i++) last.set(s[i], i);
const sizes: number[] = [];
let start = 0;
let end = 0;
for (let i = 0; i < s.length; i++) {
end = Math.max(end, last.get(s[i]) as number);
if (i === end) {
sizes.push(end - start + 1);
start = i + 1;
}
}
return sizes;
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
Lemonade costs $5 and customers pay with $5, $10 or $20 bills in order. Can you give every customer correct change?
Giving "change": always hand back a $10 before two $5s, because $5s are more flexible.
Not attempted yet
You may complete as many transactions as you like, holding at most one share at a time. Return the maximum profit.
"As many transactions" as you like: add up every positive day-to-day difference.
Not attempted yet
Each element is your maximum jump length from that position. Can you reach the last index?
"Jump" and "reach the last index": track the farthest reachable index; fail if you outrun it.
Not attempted yet
Return the minimum number of jumps to reach the last index, which is always reachable.
"Minimum number of" jumps: BFS by levels without a queue. Each jump covers a window; the next window ends at the farthest reach inside it.
Not attempted yet
Stations on a circuit have gas and the cost to the next station. Return the starting station to travel around once, or -1.
"Gas station" "circuit": if total gas ≥ total cost a start exists; reset after any point the tank goes negative.
Not attempted yet
Can the cards be rearranged into groups of size groupSize, each of consecutive values?
"Groups of size" k of "consecutive" cards: the smallest card left must start a group, so count, sort, and consume runs.
Not attempted yet
Partition the string into as many parts as possible so each letter appears in at most one part. Return the part sizes.
"As many parts as possible": record each letter's last index and close a part when you reach the furthest one seen.
Not attempted yet
A string has '(', ')' and '*', where '*' is a wildcard that can be '(', ')' or empty. Is it valid?
A "wildcard" makes the open count a range: track the min and max possible opens; fail if max goes negative, clamp min at zero.
Not attempted yet
Merging two triplets takes the max of each position. Can you form the target triplet by merging some of the given triplets?
Ignore any triplet with a value above the target; check the rest cover each target position.
Not attempted yet
Children stand in a line with ratings. Each gets at least one candy, and a child rated higher than a neighbour gets more candies. Return the minimum total.
"Candies" relative to both neighbours: one left-to-right pass, one right-to-left pass, take the max of each.
Not attempted yet