-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathNon-Constructible Change.py
36 lines (24 loc) · 1.01 KB
/
Non-Constructible Change.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
Non-Constructible Change
Given an array of positive integers representing the values of coins in your
possession,write a function that returns the minimum amount of change(the minimum sum
of money) that you cannot create.The given coins can have any positive integer value
and aren't necessarily unique (i.e you can have multiple coins and of the same value).
For example,if you're given coins = [1, 2, 5], the minimum amount of change that you
can't create is 4. If you're given no coins,the minimum amount of change that you can't
create is 1.
Sample Input
coins = [5, 7, 1, 1, 2, 3 , 22]
Sample Output
20
"""
# SOLUTION 1
# O(nlogn) time | O(1) space where n is the number of coins
def nonConstructibleChange(coins):
coins.sort()
currentChangeCreated = 0
for coin in coins:
if coin > currentChangeCreated + 1:
return currentChangeCreated + 1
currentChangeCreated += coin
return currentChangeCreated + 1