-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJW_118668.java
More file actions
40 lines (38 loc) ยท 1.61 KB
/
JW_118668.java
File metadata and controls
40 lines (38 loc) ยท 1.61 KB
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
import java.util.Arrays;
class JW_118668 {
public int solution(int alp, int cop, int[][] problems) {
// ๋ชจ๋ ๋ฌธ์ ๋ฅผ ํ๊ธฐ ์ํ ์ต์๊ฐ์ ์ฐพ๊ธฐ ์ํด ์ด๊ธฐํ
int maxAlp = alp, maxCop = cop;
for (int i = 0; i < problems.length; i++) {
maxAlp = Math.max(maxAlp, problems[i][0]);
maxCop = Math.max(maxCop, problems[i][1]);
}
// DP ๋ฐฐ์ด ์ด๊ธฐํ
int[][] dp = new int[maxAlp + 1][maxCop + 1];
for (int i = 0; i < maxAlp + 1; i++)
Arrays.fill(dp[i], Integer.MAX_VALUE >> 2);
dp[alp][cop] = 0;
// DP
// ํ์ฌ ๊ฐ ๊ธฐ์ค์ผ๋ก ๋ค์ ๊ฐ ๊ฒฐ์ ํ๊ธฐ
for (int i = alp; i < maxAlp + 1; i++) {
for (int j = cop; j < maxCop + 1; j++) {
// ์๊ณ ๋ ฅ ์ฆ๊ฐ
if (i < maxAlp)
dp[i + 1][j] = Math.min(dp[i + 1][j], dp[i][j] + 1);
// ์ฝ๋ฉ๋ ฅ ์ฆ๊ฐ
if (j < maxCop)
dp[i][j + 1] = Math.min(dp[i][j + 1], dp[i][j] + 1);
// ๋ฌธ์ ํ๊ธฐ
for (int[] problem : problems)
// ํ ์ ์๋ ๋ฌธ์ ๋ผ๋ฉด
if (i >= problem[0] && j >= problem[1]) {
// ๋ฅ๋ ฅ์น ์ฆ๊ฐ
int nextAlp = Math.min(maxAlp, i + problem[2]);
int nextCop = Math.min(maxCop, j + problem[3]);
dp[nextAlp][nextCop] = Math.min(dp[nextAlp][nextCop], dp[i][j] + problem[4]);
}
}
}
return dp[maxAlp][maxCop];
}
}