You're running a cash register and need to make change for a certain amount amount
.
You can only use certain denominations of coins, which are given in the array values
. For example, you might only have 3 dollar coins and 9 dollar coins.
Return the total number of ways you can hand customers change for the given amount.
Example:
Explanation: There are 5 ways you can hand the customer their change. They are (1+1+1+1), (2+1+1), (1+2+1), (1+1+2), and (2+2).
To solve this problem, we want to find the number of ways to hand the customer change for the given amount.
1. Recursion
In order to make change for an amount you need to hand the customer one of the values that's available to you in the values
array. For example, you might hand the customer a 2 dollar coin. You can find the number of ways to hand them a 2 dollar coin like this:
To find the recursion, you have to sum over all possible ways of handing the customer their change:
2. Base case
The recursion will call itself on smaller and smaller amounts, until it eventually reaches 0 and even negative amounts. There are no ways to make change for a negative amount, and there's only 1 way to get to the amount 0, since we always start with it.
3. Code
We could naively code this up (spoiler below), but this recursion has many redundant calls.
You'll get Time Limit Exceeded if you run this.
This means we need to use Dynamic Programming to optimize. numWays(amount) only depends on numWays(smaller amounts), so we can compute numWays on smaller amounts first and bigger ones last. This is called "Tabulation". We'll take care of the numWays(amount < 0)
base case in the sum, since it's not really possible to take care of anywhere else.
Time Complexity O(amount⋅∣values∣)
Space Complexity O(amount)
TreeNode.of
function can be used to create an entire binary tree using 1 line of code. The input is a BFS traversal of the tree including nulls, and the output is the root node of the tree.TreeNode.of([1, None, 2, None, 3])
ListNode.of
function lets you create an entire Linked List using 1 line of code. The input is an array, and the output is the head of the Linked List.ListNode.of([1, 2, 3])
ListNode.of([1, 2, 3], 1)
GraphNode.of
function lets you create an entire Graph using 1 line of code. The input is an Adjacency Matrix, and the output is the first node in the matrix.GraphNode.of([ ['A', 1, 2], ['B', 2], ['C', 0] ])
[['__init__', 15], ['add', 16], ['get']]
c = MyClass(15) c.add(16) c.get()
TreeNode.of
function can be used to create an entire binary tree using 1 line of code. The input is a BFS traversal of the tree including nulls, and the output is the root node of the tree.TreeNode.of([1, None, 2, None, 3])
ListNode.of
function lets you create an entire Linked List using 1 line of code. The input is an array, and the output is the head of the Linked List.ListNode.of([1, 2, 3])
ListNode.of([1, 2, 3], 1)
GraphNode.of
function lets you create an entire Graph using 1 line of code. The input is an Adjacency Matrix, and the output is the first node in the matrix.GraphNode.of([ ['A', 1, 2], ['B', 2], ['C', 0] ])
[['__init__', 15], ['add', 16], ['get']]
c = MyClass(15) c.add(16) c.get()