public int solution(int temperature, int t1, int t2, int a, int b, int[] onboard) {
int temp = temperature > t2 ? t1 - (temperature - t2) : temperature;
int INF = 100001;
t1 -= temp;
t2 -= temp;
int n = onboard.length;
int[][] dp = new int[n][t2 + 2];
for (int i = 0; i < n; i++)
Arrays.fill(dp[i], INF);
dp[0][0] = 0;
for (int i = 1; i < n; i++) {
for (int j = 0; j <= t2 + 1; j++) {
if (onboard[i] == 1 && (j < t1 || j > t2))
continue;
int min = INF;
if (j == 0) {
min = Math.min(min, dp[i - 1][j]);
if (j + 1 <= t2 + 1)
min = Math.min(min, dp[i - 1][j + 1]);
} else {
int prev1 = j - 1 >= 0 ? dp[i - 1][j - 1] + a : INF;
int prev2 = dp[i - 1][j] + b;
int prev3 = j + 1 <= t2 + 1 ? dp[i - 1][j + 1] : INF;
min = Math.min(min, Math.min(prev1, Math.min(prev2, prev3)));
}
dp[i][j] = min;
}
}
int result = Integer.MAX_VALUE;
for (int j = 0; j <= t2 + 1; j++)
result = Math.min(result, dp[n - 1][j]);
return result;
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/214289