forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination Sum.py
More file actions
121 lines (108 loc) · 4.11 KB
/
Combination Sum.py
File metadata and controls
121 lines (108 loc) · 4.11 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Combination Sum
#https://leetcode.com/problems/combination-sum/
#Wrong logic
class Solution1(object):
def __init__(self):
self.ht = {}
def putInHash(self, candidates):
for num in candidates:
if not(num in self.ht):
self.ht[num] = True
def logic(self, candidates, target):
output = []
for num in candidates:
if(num == target):
output.append([num])
continue
print "Number ", num
temp = [num]
targetTemp = target
print "Temp array ", temp, " Temporary Target ", targetTemp
while(targetTemp-num > 0):
print "TargetTemp - num ", targetTemp-num
if(targetTemp-num in self.ht):
print "Target Temp - num present in hash"
temp.append(targetTemp-num)
#this step is performed to avoid duplicate inserts.
temp.sort()
print "Temp array now is ", temp
if not(temp in output):
output.append(temp[:])
print "Appending temp array to output ", output
temp.pop()
print "Temp Array now is ", temp
temp.append(num)
print "Temp array after appending number ", temp
targetTemp = targetTemp - num
print "Temp target is ", targetTemp
print output
def combinationSum(self, candidates, target):
self.putInHash(candidates)
self.logic(candidates, target)
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
#Wrong solution
class Solution2(object):
def __init__(self):
self.hash = {}
def addToHash(self, key, value):
if not key in self.hash:
self.hash[key] = [value]
else:
self.hash[key].append(value)
def putInHash(self, candidates, target):
for num in candidates:
self.addToHash(num, [num])
tempNum = num
tempArr = [num]
while(tempNum+num < target):
tempNum = tempNum + num
tempArr.append(num)
self.addToHash(tempNum, tempArr[:])
def logic(self, candidates, target):
for num in candidates:
if(num == target):
output.append([num])
continue
temp = [num]
targetTemp = target
while(targetTemp-num > 0):
if(targetTemp-num in self.hash):
print self.hash[targetTemp-num], temp
temp.append(num)
targetTemp = targetTemp-num
def combinationSum(self, candidates, target):
self.putInHash(candidates, target)
self.logic(candidates, target)
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
class Solution3(object):
def logic(self, candidates, target, output, finalResult, index):
if(sum(output) > target):
return
if(sum(output) == target):
finalResult.append(output)
return
for i in range(index, len(candidates)):
if(sum(output)+candidates[i] <= target):
#notice output+[num] thats for appending to the list on the fly so we dont need
#extra line for backtracking
self.logic(candidates, target, output+[candidates[i]], finalResult, i)
def combinationSum(self, candidates, target):
#notice finalResult ? we do that so we dont need global variable to store output
finalResult = []
self.logic(candidates, target, [], finalResult, 0)
print finalResult
#Main
obj = Solution3()
obj.combinationSum([2,3,5], 8)
obj = Solution3()
obj.combinationSum([2,3,6,7], 7)
obj = Solution3()
obj.combinationSum([7,3,2], 18)