2D dp
68.63%
public int putBox(int[] box, int[] position) {
// Write your code herel
// dp[i][j] max count of box that can fit for box [0, i) and position [0, j)
int[] prevDP = null;
for (int j = 0; j <= position.length; j++) {
int[] dp = new int[box.length + 1];
for (int i = 0; i <= box.length; i++) {
if (i == 0 || j == 0) {
dp[i] = 0;
} else {
dp[i] = Math.max(dp[i - 1], prevDP[i]);
if (box[i - 1] <= position[j - 1]) {
dp[i] = Math.max(dp[i], 1 + prevDP[i - 1]);
}
}
}
prevDP = dp;
}
return prevDP[box.length];
}
No comments:
Post a Comment