Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
243 lines (200 loc) · 6.5 KB

File metadata and controls

243 lines (200 loc) · 6.5 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package Algorithms.algorithm.dp;
import java.util.ArrayList;
public class MinAdjustmentCost_Class {
public static void main(String[] strs) {
ArrayList<Integer> A = new ArrayList<Integer>();
A.add(1);
A.add(4);
A.add(2);
A.add(3);
System.out.println(MinAdjustmentCost1(A, 1));
}
/**
* @param A: An integer array.
* @param target: An integer.
*/
public static int MinAdjustmentCost1(ArrayList<Integer> A, int target) {
// write your code here
if (A == null) {
return 0;
}
ArrayList<Integer> B = new ArrayList<Integer>();
for (int i = 0; i < A.size(); i++) {
B.add(0);
}
return rec(A, B, target, 0);
}
/*
* SOL 1:
* 最普通的递归方法。
* */
public static int rec(ArrayList<Integer> A, ArrayList<Integer> B, int target, int index) {
int len = A.size();
if (index >= len) {
// The index is out of range.
return 0;
}
int dif = 0;
int min = Integer.MAX_VALUE;
// If this is the first element, it can be from 1 to 100;
for (int i = 0; i <= 100; i++) {
if (index != 0 && Math.abs(i - B.get(index - 1)) > target) {
continue;
}
B.set(index, i);
dif = Math.abs(i - A.get(index));
dif += rec(A, B, target, index + 1);
min = Math.min(min, dif);
// 回溯
//B.set(index, A.get(index));
}
return min;
}
/*
* 递归2:
* Rec + memory.
* */
/**
* @param A: An integer array.
* @param target: An integer.
*/
public static int MinAdjustmentCost2(ArrayList<Integer> A, int target) {
// write your code here
if (A == null || A.size() == 0) {
return 0;
}
int[][] M = new int[A.size()][100];
for (int i = 0; i < A.size(); i++) {
for (int j = 0; j < 100; j++) {
M[i][j] = Integer.MAX_VALUE;
}
}
return rec2(A, new ArrayList<Integer>(A), target, 0, M);
}
public static int rec2(ArrayList<Integer> A, ArrayList<Integer> B, int target, int index,
int[][] M) {
int len = A.size();
if (index >= len) {
// The index is out of range.
return 0;
}
int dif = 0;
int min = Integer.MAX_VALUE;
// If this is the first element, it can be from 1 to 100;
for (int i = 1; i <= 100; i++) {
if (index != 0 && Math.abs(i - B.get(index - 1)) > target) {
continue;
}
if (M[index][i - 1] != Integer.MAX_VALUE) {
dif = M[index][i - 1];
min = Math.min(min, dif);
continue;
}
B.set(index, i);
dif = Math.abs(i - A.get(index));
dif += rec2(A, B, target, index + 1, M);
min = Math.min(min, dif);
// Record the result.
M[index][i - 1] = dif;
// 回溯
B.set(index, A.get(index));
}
return min;
}
/*
* SOLUTION 3 递归2:
* Rec + memory.
* 改进的递归版本
* */
/**
* @param A: An integer array.
* @param target: An integer.
*/
public static int MinAdjustmentCost3(ArrayList<Integer> A, int target) {
// write your code here
if (A == null || A.size() == 0) {
return 0;
}
int[][] M = new int[A.size()][100];
for (int i = 0; i < A.size(); i++) {
for (int j = 0; j < 100; j++) {
M[i][j] = Integer.MAX_VALUE;
}
}
// 首个数字可以取1-100
int min = Integer.MAX_VALUE;
for (int i = 1; i <= 100; i++) {
min = Math.min(min, rec3(A, target, 0, i, M));
}
return min;
}
/*
* 将当前值设置为x能求得的最小解
* */
public static int rec3(ArrayList<Integer> A, int target, int index, int x,
int[][] M) {
int len = A.size();
if (index >= len) {
// The index is out of range.
return 0;
}
if (M[index][x - 1] != Integer.MAX_VALUE) {
return M[index][x - 1];
}
int bas = Math.abs(x - A.get(index));
int min = Integer.MAX_VALUE;
// 对下一个值尝试取1-100
for (int i = 1; i <= 100; i++) {
// 下一个值的取值不可以超过abs
if (index != len - 1 && Math.abs(i - x) > target) {
continue;
}
// 计算dif
int dif = bas + rec3(A, target, index + 1, i, M);
min = Math.min(min, dif);
}
// Record the result.
M[index][x - 1] = min;
return min;
}
/*
* SOLUTION 4:
* DP
* */
/**
* @param A: An integer array.
* @param target: An integer.
*/
public static int MinAdjustmentCost(ArrayList<Integer> A, int target) {
// write your code here
if (A == null || A.size() == 0) {
return 0;
}
// D[i][v]: 把index = i的值修改为v,所需要的最小花费
int[][] D = new int[A.size()][101];
int size = A.size();
for (int i = 0; i < size; i++) {
for (int j = 1; j <= 100; j++) {
D[i][j] = Integer.MAX_VALUE;
if (i == 0) {
// The first element.
D[i][j] = Math.abs(j - A.get(i));
} else {
for (int k = 1; k <= 100; k++) {
// 不符合条件
if (Math.abs(j - k) > target) {
continue;
}
int dif = Math.abs(j - A.get(i)) + D[i - 1][k];
D[i][j] = Math.min(D[i][j], dif);
}
}
}
}
int ret = Integer.MAX_VALUE;
for (int i = 1; i <= 100; i++) {
ret = Math.min(ret, D[size - 1][i]);
}
return ret;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.