/322. Coin Change

322. Coin Change

Medium
Arrays48% acceptance

Given an integer array denominations representing available coin values and an integer target_sum representing the desired sum, return the minimum number of coins required to reach target_sum. If it is not possible to reach target_sum using any combination of denominations, return -1. Assume unlimited supply of each denomination.

Example 1

Input: denominations = [3, 7, 10], target_sum = 14

Output: 2

Explanation: 14 = 7 + 7

Example 2

Input: denominations = [4, 6], target_sum = 5

Output: -1

Explanation: Cannot make 5 with 4 and 6

Example 3

Input: denominations = [2], target_sum = 0

Output: 0

Explanation: Zero coins needed for sum zero

Constraints

  • 1 <= len(denominations) <= 12
  • 1 <= denominations[i] <= 231 - 1
  • 0 <= target_sum <= 104
Python (current runtime)

Case 1

Input: denominations = [2, 3, 6], target_sum = 7

Expected: 2

Case 2

Input: denominations = [5, 9], target_sum = 18

Expected: 2

Case 3

Input: denominations = [1, 4, 5], target_sum = 8

Expected: 2

Case 4

Input: denominations = [8], target_sum = 7

Expected: -1

Case 5

Input: denominations = [1, 2, 3], target_sum = 0

Expected: 0