-
Notifications
You must be signed in to change notification settings - Fork 0
/
316.py
61 lines (49 loc) · 1.77 KB
/
316.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Problem:
You are given an array of length N, where each element i represents the number of ways
we can produce i units of change. For example, [1, 0, 1, 1, 2] would indicate that
there is only one way to make 0, 2, or 3 units, and two ways of making 4 units.
Given such an array, determine the denominations that must be in use. In the case
above, for example, there must be coins with value 2, 3, and 4.
"""
from typing import List
def count_ways_to_generate_change(changes: List[int], target: int) -> int:
length = len(changes)
if not length:
return 0
table = [[0 for x in range(length)] for x in range(target + 1)]
for i in range(length):
table[0][i] = 1
for i in range(1, target + 1):
for j in range(length):
if i - changes[j] >= 0:
x = table[i - changes[j]][j]
else:
x = 0
if j >= 1:
y = table[i][j - 1]
else:
y = 0
table[i][j] = x + y
return table[target][length - 1]
def get_changes(num_ways_to_get_change: List[int]) -> List[int]:
length = len(num_ways_to_get_change)
changes_list = []
for i in range(1, length):
if num_ways_to_get_change[i] > 0:
count = count_ways_to_generate_change(changes_list, i)
if count == 0 or count + 1 == num_ways_to_get_change[i]:
changes_list.append(i)
return changes_list
if __name__ == "__main__":
print(get_changes([1, 0, 1, 1, 2]))
print(get_changes([1, 0, 1, 1, 2, 1, 3]))
print(get_changes([1, 0, 1, 1, 2, 1, 4]))
print(get_changes([1, 0, 1, 1, 2, 1, 4, 2]))
print(get_changes([1, 0, 1, 1, 2, 1, 4, 3]))
"""
SPECS:
TIME COMPLEXITY: O(n ^ 3)
SPACE COMPLEXITY: O(n ^ 2)
[n = number of elements]
"""