FastPrepMinimum Cash Pieces with Limited Inventory
Problem · Array

Minimum Cash Pieces with Limited Inventory

Learn this problem
MediumAmazon logoAmazonNEW GRADONSITE INTERVIEW
See Amazon hiring insights

Problem statement

A cash drawer contains bills and coins in the fixed denomination order [2000, 1000, 500, 100, 25, 10, 5, 1], measured in cents. The value inventory[i] is the number of available pieces of denomination i.

Given amountCents, return the minimum number of available bills and coins needed to make that amount exactly. Each piece may be used at most once.

Return -1 when the drawer cannot make exact change. The amount is already represented in cents, so no floating-point arithmetic is needed.

Function

minimumCashPieces(amountCents: int, inventory: int[]) → int

Examples

Example 1

amountCents = 635inventory = [1,1,1,1,1,1,1,100]return = 4

Use one 500-cent bill, one 100-cent bill, one quarter, and one dime for 4 pieces.

Example 2

amountCents = 30inventory = [0,0,0,0,1,3,0,0]return = 3

Using the quarter would leave 5 cents, which cannot be formed. Three dimes make exact change with 3 pieces.

Example 3

amountCents = 3inventory = [0,0,0,0,0,0,0,2]return = -1

The drawer contains only two pennies, so it cannot make 3 cents exactly.

Constraints

  • inventory.length == 8.
  • 0 <= amountCents <= 10^5.
  • 0 <= inventory[i] <= 10^4.

More Amazon problems

drafts saved locally
public int minimumCashPieces(int amountCents, int[] inventory) {
  // Write your code here
}
amountCents635
inventory[1,1,1,1,1,1,1,100]
expected4
checking account