Make the locally optimal choice at every step and hope it leads to a global optimum. Cheap but only works when the problem has a greedy-choice property and optimal substructure.
intervals: (1,3), (2,4), (3,5), (6,7)
sort by end: (1,3), (3,5), (2,4), (6,7)
greedy: pick shortest-finishing job that starts >= previous end
select (1,3) -> (3,5) -> (6,7)
max non-overlapping: 3
{1,3,4} for amount 6: greedy picks 4 + 1 + 1 = 3 coins, optimal is 3 + 3 = 2.def can_jump(nums):
furthest = 0
for i, n in enumerate(nums):
if i > furthest: return False
furthest = max(furthest, i + n)
return True
Driving a rental car back to the depot: always fill up at the cheapest station you can reach. It minimises total cost ONLY if prices are non-increasing.
Interview tip: If you cannot prove greedy correct within ~30 seconds, downswitch to DP.