Greedy Algorithms8 sections · 316 units
Open in Course

Lemonade - The Idea

Prefer giving $10 bills

When a customer pays 20,youneedtogive20, you need to give 15 change. You have two options:

1.1. One 10andone10 and one 5

2.2. Three 5billsWhichisbetter?Alwayspreferoption1(usea5 bills Which is better? Always prefer option 1 (use a 10). Why? The 5billismoreversatile.Itcanmakechangefor5 bill is more versatile. It can make change for 10 customers. The 10billcanonlyhelpwith10 bill can only help with 20 customers. Save the flexible currency. Here's greedy: use the least flexible bills first. This "save the flexible resource" pattern shows up in many greedy problems. When multiple choices work, pick the one that preserves your options.