Skip to the code
cf.
+10 XP

#572 Make Change at a Pop-Up Stand

easy

A pop-up stand's point-of-sale app sells one item for 5 dollars. Customers pay in order, each with a single 5, 10, or 20 dollar bill, listed in bills. The register starts empty and must give each customer exact change from the bills it has collected so far.

Write canMakeChange(bills) that returns true if every customer gets correct change, and false otherwise.

Example 1

Input: bills = [5,5,5,10,20]
Output: true

The 10 gets a 5 back, and the 20 gets a 10 and a 5 back.

Example 2

Input: bills = [5,5,10,10,20]
Output: false

The last customer needs 15 back, but the register only holds two 10s.

Constraints

  • 0 <= bills.length <= 100000
  • Each bill is 5, 10, or 20.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).