Skip to content

Commit 752eae9

Browse files
committed
modify code
1 parent 3554ba5 commit 752eae9

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/class42/Code02_ThrowChessPiecesProblem.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,26 @@ public static int superEggDrop3(int kChess, int nLevel) {
6565
for (int i = 1; i != dp.length; i++) {
6666
dp[i][1] = i;
6767
}
68-
int[] cands = new int[kChess + 1];
68+
int[][] best = new int[nLevel + 1][kChess + 1];
6969
for (int i = 1; i != dp[0].length; i++) {
7070
dp[1][i] = 1;
71-
cands[i] = 1;
71+
best[1][i] = 1;
7272
}
7373
for (int i = 2; i < nLevel + 1; i++) {
7474
for (int j = kChess; j > 1; j--) {
75-
int min = Integer.MAX_VALUE;
76-
int minEnum = cands[j];
77-
int maxEnum = j == kChess ? i / 2 + 1 : cands[j + 1];
78-
for (int k = minEnum; k < maxEnum + 1; k++) {
79-
int cur = Math.max(dp[k - 1][j - 1], dp[i - k][j]);
80-
if (cur <= min) {
81-
min = cur;
82-
cands[j] = k;
75+
int ans = Integer.MAX_VALUE;
76+
int bestChoose = -1;
77+
int down = best[i - 1][j];
78+
int up = j == kChess ? i : best[i][j + 1];
79+
for (int first = down; first <= up; first++) {
80+
int cur = Math.max(dp[first - 1][j - 1], dp[i - first][j]);
81+
if (cur <= ans) {
82+
ans = cur;
83+
bestChoose = first;
8384
}
8485
}
85-
dp[i][j] = min + 1;
86+
dp[i][j] = ans + 1;
87+
best[i][j] = bestChoose;
8688
}
8789
}
8890
return dp[nLevel][kChess];

0 commit comments

Comments
 (0)