Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit499a088

Browse files
committed
dp
1 parent41a5c04 commit499a088

File tree

1 file changed

+114
-1
lines changed

1 file changed

+114
-1
lines changed

‎algorithm/dp/MinAdjustmentCost_Class.java

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static int rec(ArrayList<Integer> A, ArrayList<Integer> B, int target, in
6767
* @param A: An integer array.
6868
* @param target: An integer.
6969
*/
70-
publicstaticintMinAdjustmentCost(ArrayList<Integer>A,inttarget) {
70+
publicstaticintMinAdjustmentCost2(ArrayList<Integer>A,inttarget) {
7171
// write your code here
7272
if (A ==null ||A.size() ==0) {
7373
return0;
@@ -121,5 +121,118 @@ public static int rec2(ArrayList<Integer> A, ArrayList<Integer> B, int target, i
121121

122122
returnmin;
123123
}
124+
125+
/*
126+
* SOLUTION 3 递归2:
127+
* Rec + memory.
128+
* 改进的递归版本
129+
* */
130+
/**
131+
* @param A: An integer array.
132+
* @param target: An integer.
133+
*/
134+
publicstaticintMinAdjustmentCost3(ArrayList<Integer>A,inttarget) {
135+
// write your code here
136+
if (A ==null ||A.size() ==0) {
137+
return0;
138+
}
139+
140+
int[][]M =newint[A.size()][100];
141+
for (inti =0;i <A.size();i++) {
142+
for (intj =0;j <100;j++) {
143+
M[i][j] =Integer.MAX_VALUE;
144+
}
145+
}
146+
147+
// 首个数字可以取1-100
148+
intmin =Integer.MAX_VALUE;
149+
for (inti =1;i <=100;i++) {
150+
min =Math.min(min,rec3(A,target,0,i,M));
151+
}
152+
153+
returnmin;
154+
}
155+
156+
/*
157+
* 将当前值设置为x能求得的最小解
158+
* */
159+
publicstaticintrec3(ArrayList<Integer>A,inttarget,intindex,intx,
160+
int[][]M) {
161+
intlen =A.size();
162+
if (index >=len) {
163+
// The index is out of range.
164+
return0;
165+
}
166+
167+
if (M[index][x -1] !=Integer.MAX_VALUE) {
168+
returnM[index][x -1];
169+
}
170+
171+
intbas =Math.abs(x -A.get(index));
172+
intmin =Integer.MAX_VALUE;
173+
174+
// 对下一个值尝试取1-100
175+
for (inti =1;i <=100;i++) {
176+
// 下一个值的取值不可以超过abs
177+
if (index !=len -1 &&Math.abs(i -x) >target) {
178+
continue;
179+
}
180+
181+
// 计算dif
182+
intdif =bas +rec3(A,target,index +1,i,M);
183+
min =Math.min(min,dif);
184+
}
185+
186+
// Record the result.
187+
M[index][x -1] =min;
188+
returnmin;
189+
}
124190

191+
192+
/*
193+
* SOLUTION 4:
194+
* DP
195+
* */
196+
/**
197+
* @param A: An integer array.
198+
* @param target: An integer.
199+
*/
200+
publicstaticintMinAdjustmentCost(ArrayList<Integer>A,inttarget) {
201+
// write your code here
202+
if (A ==null ||A.size() ==0) {
203+
return0;
204+
}
205+
206+
// D[i][v]: 把index = i的值修改为v,所需要的最小花费
207+
int[][]D =newint[A.size()][101];
208+
209+
intsize =A.size();
210+
211+
for (inti =0;i <size;i++) {
212+
for (intj =1;j <=100;j++) {
213+
D[i][j] =Integer.MAX_VALUE;
214+
if (i ==0) {
215+
// The first element.
216+
D[i][j] =Math.abs(j -A.get(i));
217+
}else {
218+
for (intk =1;k <=100;k++) {
219+
// 不符合条件
220+
if (Math.abs(j -k) >target) {
221+
continue;
222+
}
223+
224+
intdif =Math.abs(j -A.get(i)) +D[i -1][k];
225+
D[i][j] =Math.min(D[i][j],dif);
226+
}
227+
}
228+
}
229+
}
230+
231+
intret =Integer.MAX_VALUE;
232+
for (inti =1;i <=100;i++) {
233+
ret =Math.min(ret,D[size -1][i]);
234+
}
235+
236+
returnret;
237+
}
125238
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp