-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrencyConvertProblem.java
More file actions
256 lines (133 loc) · 3.45 KB
/
CurrencyConvertProblem.java
File metadata and controls
256 lines (133 loc) · 3.45 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
public class CurrencyConvertProblem{
public static int currencyConvert1(int[] arr,int aim){
return process(arr,0,aim);
}
public static int process(int[] arr,int index,int rest){
if(rest == 0){
return 1;
}
if(rest < 0 ){
return 0;
}
int sum = 0;
for (int i=index; i<arr.length;i++ ) {
sum+= process(arr,i+1,rest-arr[i]);
}
return sum;
}
public static int currencyConvert2(int[] arr,int aim){
return process2(arr,0,aim);
}
public static int process2(int[] arr,int index,int rest){
if(rest == 0){
return 1;
}
if(rest < 0 || index==arr.length){
return 0;
}
return process2(arr,index+1,rest) + process2(arr,index+1,rest-arr[index]);
}
public static int currencyConvert3(int[] arr,int aim){
int N = arr.length;
int[][] dp=new int[N+1][aim+1];
for (int i=0; i<=N; i++) {
for (int j=0; j<=aim; j++) {
dp[i][j] = -1;
}
}
return process3(arr,0,aim,dp);
}
public static int process3(int[] arr,int index,int rest,int[][] dp){
if(rest == 0){
return 1;
}
else
if(rest < 0 ){
return 0;
}
if(dp[index][rest] != -1){
return dp[index][rest];
}
int sum = 0;
for (int i=index; i<arr.length;i++ ) {
sum+= process3(arr,i+1,rest-arr[i],dp);
}
dp[index][rest] = sum;
return sum;
}
public static int currencyConvert4(int[] arr,int aim){
int N = arr.length;
int[][] dp=new int[N+1][aim+1];
for (int i=0; i<=N; i++) {
dp[i][0] = 1;
}
for (int index=N-1; index>=0; index--) {
for (int rest=0; rest<=aim; rest++) {
dp[index][rest] = dp[index+1][rest]+
((rest-arr[index] >=0)?dp[index+1][rest-arr[index]]:0);
}
}
return dp[0][aim];
}
public static int currencyConvert5(int[] arr,int aim){
return process5(arr,0,aim);
}
public static int process5(int[] arr,int index,int rest){
if(rest == 0){
return 1;
}
int sum = 0;
for (int i=index; i<arr.length&&rest-arr[i]>=0;i++ ) {
sum+= process5(arr,i,rest-arr[i]);
}
return sum;
}
public static int currencyConvert6(int[] arr,int aim){
return process6(arr,0,aim);
}
public static int process6(int[] arr,int index,int rest){
if(index == arr.length){
return rest==0?1:0;
}
int sum = 0;
for (int i=0; i*arr[index]<=rest;i++ ) {
sum+= process6(arr,index+1,rest-(i*arr[index]));
}
return sum;
}
public static int currencyConvert7(int[] arr,int aim){
int N = arr.length;
int[][] dp=new int[N+1][aim+1];
for (int i=0;i<=N ; i++) {
dp[i][0] = 1;
}
for (int index=N-1; index>=0; index--) {
for (int rest=0; rest<=aim; rest++) {
int sum = 0;
for (int i=index; i<arr.length&&rest-arr[i]>=0;i++ ) {
sum+= dp[i][rest-arr[i]];
}
dp[index][rest] = sum;
}
}
for (int i=0; i<=N; i++) {
System.out.println();
for (int j=0; j<=aim; j++) {
System.out.print(dp[i][j]+",");
}
}
return dp[0][aim];
}
public static void main(String[] args){
int[] arr= new int[]{2,4,5,10};
int aim = 20;
//System.out.println("count = "+currencyConvert1(arr,aim));
//System.out.println("count = "+currencyConvert2(arr,aim));
System.out.println("count = "+currencyConvert3(arr,aim));
System.out.println("count = "+currencyConvert4(arr,aim));
System.out.println("count = "+currencyConvert5(arr,aim));
System.out.println("count = "+currencyConvert6(arr,aim));
//currencyConvert7错误
System.out.println("count = "+currencyConvert7(arr,aim));
}
}