forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinChangeSet7WithBacktracking.py
More file actions
41 lines (35 loc) · 1.08 KB
/
CoinChangeSet7WithBacktracking.py
File metadata and controls
41 lines (35 loc) · 1.08 KB
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
#Coin Change
# https://www.geeksforgeeks.org/dynamic-programming-set-7-coin-change/
class coinChange(object):
def __init__(self, N, S):
self.N = N
self.S = S
def logic_approach2(self, N, S, n, output):
if n == N:
print output
return
for i in range(len(S)):
if(S[i]+n <=N):
output.append(S[i])
self.logic_approach2(N, S, S[i]+n, output)
#Backtrack
output.pop()
#Backtrack
S.pop()
def logic_approach1(self, N, S, n, output):
if n == N:
print output
return
for i in S:
if(i+n <= N):
output.append(i)
self.logic_approach1(N, S, i+n, output)
#Backtrack
output.pop()
def solution(self):
self.logic_approach1(self.N, self.S, 0, [])
print "-----------"
self.logic_approach2(self.N, self.S, 0, [])
#Main Program
cc = coinChange(10, [2, 5, 3, 6])
cc.solution()