-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSum.java
More file actions
214 lines (200 loc) · 7.47 KB
/
Sum.java
File metadata and controls
214 lines (200 loc) · 7.47 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class Sum {
/**
* ===============================================================
* CAN SUM
* decision problem
* ===============================================================
*/
/**
* CAN SUM NAIVE IMPLEMENTATION
*/
public boolean naiveCanSum(int targetSum, List<Integer> numbers) {
if (targetSum < 0) { return false; }
if (targetSum == 0) { return true; }
for (Integer value : numbers) {
if (naiveCanSum(targetSum - value, numbers)) {
return true;
}
}
return false;
}
/**
* CAN SUM DYNAMIC IMPLEMENTATION
* USING MEMOIZATION
*/
public boolean dynamicCanSum(int targetSum, List<Integer> numbers) {
return dynamicCanSum(targetSum, numbers, new HashMap<>());
}
private boolean dynamicCanSum(int targetSum, List<Integer> numbers, HashMap<Integer, Boolean> memo) {
if (memo.containsKey(targetSum)) { return memo.get(targetSum); }
if (targetSum < 0) { return false; }
if (targetSum == 0) { return true; }
for (Integer value : numbers) {
if (dynamicCanSum(targetSum - value, numbers, memo)) {
memo.put(targetSum, true);
return true;
}
}
memo.put(targetSum, false);
return false;
}
/**
* CAN SUM DYNAMIC IMPLEMENTATION
* USING TABULATION
*/
public boolean dynamicCanSumTabular(int targetSum, List<Integer> numbers) {
boolean [] tabulation = new boolean[targetSum + 1];
tabulation[0] = true;
for (int i = 0; i < targetSum; i++) {
for (Integer number : numbers) {
if (tabulation[i] && i+number <= targetSum) {
tabulation[i+number] = true;
}
}
}
return tabulation[targetSum];
}
/**
* ===============================================================
* HOW SUM
* combinatoric problem
* ===============================================================
*/
/**
* HOW SUM NAIVE IMPLEMENTATION
*/
public List<Integer> naiveHowSum(int targetSum, List<Integer> numbers) {
if (targetSum < 0) { return null; }
if (targetSum == 0) { return (new ArrayList<>()); }
for (Integer value : numbers) {
List<Integer> result = naiveHowSum(targetSum - value, numbers);
if (result != null) {
result.add(0, value);
return result;
}
}
return null;
}
/**
* HOW SUM DYNAMIC IMPLEMENTATION
* USING MEMOIZATION
*/
public List<Integer> dynamicHowSumMemo(int targetSum, List<Integer> numbers) {
return dynamicHowSumMemo(targetSum, numbers, new HashMap<>());
}
private List<Integer> dynamicHowSumMemo(int targetSum, List<Integer> numbers, HashMap<Integer, List<Integer>> memo) {
if (memo.containsKey(targetSum)) { return memo.get(targetSum); }
if (targetSum < 0) { return null; }
if (targetSum == 0) { return (new ArrayList<>()); }
// System.out.println(memo);
for (Integer value : numbers) {
List<Integer> result = dynamicHowSumMemo(targetSum - value, numbers, memo);
if (result != null) {
result.add(0, value);
memo.put(targetSum, result);
return result;
}
}
memo.put(targetSum, null);
return null;
}
/**
* HOW SUM DYNAMIC IMPLEMENTATION
* USING TABULATION
*/
public List<Integer> dynamicHowSumTabular(int targetSum, List<Integer> numbers) {
List<List<Integer>> tabulation = new ArrayList<>();
tabulation.add(new ArrayList<>());
for (int i = 0; i < targetSum; i++) { tabulation.add(null); }
for (int i = 0; i <= targetSum; i++) {
for (Integer number : numbers) {
int index = i + number;
if (index > targetSum || tabulation.get(i) == null) { continue; }
List<Integer> list = new ArrayList<>(tabulation.get(i));
list.add(0, number);
if (index == targetSum) {
return list;
}
tabulation.set(index, list);
}
}
return null;
}
/**
* ===============================================================
* BEST SUM
* optimization problem
* ===============================================================
*/
/**
* BEST SUM NAIVE IMPLEMENTATION
*/
public List<Integer> naiveBestSum(int targetSum, List<Integer> numbers) {
if (targetSum < 0) { return null; }
if (targetSum == 0) { return new ArrayList<>(); }
List<Integer> shortest = null;
for (Integer value : numbers) {
List<Integer> combination = naiveBestSum(targetSum - value, numbers);
if (combination != null) {
combination.add(0, value);
if (shortest == null || shortest.size() > combination.size()) {
shortest = combination;
}
}
}
return shortest;
}
/**
* BEST SUM DYNAMIC IMPLEMENTATION
* USING MEMOIZATION
*/
public List<Integer> dynamicBestMemo(int targetSum, List<Integer> numbers) {
return dynamicBestSumMemo(targetSum, numbers, new HashMap<>());
}
private List<Integer> dynamicBestSumMemo(int targetSum, List<Integer> numbers, HashMap<Integer, List<Integer>> memo) {
if (memo.containsKey(targetSum)) { return memo.get(targetSum); }
if (targetSum < 0) { return null; }
if (targetSum == 0) { return new ArrayList<>(); }
List<Integer> shortest = null;
for (Integer value : numbers) {
List<Integer> combination = dynamicBestSumMemo(targetSum - value, numbers, memo);
if (combination != null) {
List<Integer> list = new ArrayList<>();
list.add(value);
list.addAll(combination);
if (shortest == null || shortest.size() > list.size()) {
shortest = list;
}
}
}
memo.put(targetSum, shortest);
return shortest;
}
/**
* BEST SUM DYNAMIC IMPLEMENTATION
* USING TABULATION
*/
public List<Integer> dynamicBestSumTabular(int targetSum, List<Integer> numbers) {
List<List<Integer>> tabulation = new ArrayList<>();
tabulation.add(new ArrayList<>());
for (int i = 0; i < targetSum; i++) { tabulation.add(null); }
for (int i = 0; i <= targetSum; i++) {
for (Integer number : numbers) {
int index = i + number;
if (index > targetSum || tabulation.get(i) == null) {
continue;
}
List<Integer> list = new ArrayList<>(tabulation.get(i));
list.add(0, number);
if (tabulation.get(index) == null || tabulation.get(index).size() > list.size()) {
tabulation.set(index, list);
}
}
}
return tabulation.get(targetSum);
}
}