Minimum Cash Pieces with Limited Inventory
Updated
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[]) → intExamples
Example 1
amountCents = 635inventory = [1,1,1,1,1,1,1,100]return = 4Use 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 = 3Using 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 = -1The 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 Onsite Interview questions
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dungeon GameONSITE INTERVIEW · Seen Nov 2026
- Integer BreakONSITE INTERVIEW · Seen Nov 2026
- LFU CacheONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
- First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
- 0/1 Knapsack with Weight and Volume LimitsGoogle · ONSITE INTERVIEWGoogle interview questions
- Maximum Subarray Sum with One DeletionOracle · ONSITE INTERVIEWOracle interview questions
- Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Coin ChangeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Maximum SubarrayUpstart · ONSITE INTERVIEWUpstart interview questions
- Smallest Sufficient TeamExpedia · ONSITE INTERVIEWExpedia interview questions
- Maximum Product SubarrayHartford Financial Services · ONSITE INTERVIEWHartford Financial Services interview questions
- Word BreakMoveworks · ONSITE INTERVIEWMoveworks interview questions
- Maximum Value from Circular HousesDigitalOcean · ONSITE INTERVIEWDigitalOcean interview questions