-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHW_118668.java
More file actions
54 lines (44 loc) ยท 1.81 KB
/
HW_118668.java
File metadata and controls
54 lines (44 loc) ยท 1.81 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
class HW_118668 {
public int solution(int alp, int cop, int[][] problems) {
int tA = alp;
int tC = cop;
// ๋ชฉํ ์๊ณ ๋ ฅ๊ณผ ์ฝ๋ฉ๋ ฅ์ ์ค์
for (int i = 0; i < problems.length; i++) {
tA = Math.max(tA, problems[i][0]); // target Alp
tC = Math.max(tC, problems[i][1]); // target Cop
}
int[][] dp = new int[tA + 1][tC + 1]; // dp[์๊ณ ๋ ฅ][์ฝ๋ฉ๋ ฅ]
// dp๋ฐฐ์ด ์ด๊ธฐํ
for (int i = 0; i <= tA; i++) {
for (int j = 0; j <= tC; j++) {
dp[i][j] = Integer.MAX_VALUE; // ์ด๊ธฐ ์ํ์์ ๋๋ฌํ ์ ์๋ ๊ฐ๋ค์ ํฐ ๊ฐ์ผ๋ก ์ด๊ธฐํ
}
}
dp[alp][cop] = 0; // ์์ ์ง์ ์๊ฐ 0์ผ๋ก ์ค์
for (int i = alp; i <= tA; i++) {
for (int j = cop; j <= tC; j++) {
if (i + 1 <= tA) { // ์๊ณ ๋ ฅ +1 ๊ฒฝ์ฐ
dp[i + 1][j] = Math.min(dp[i + 1][j], dp[i][j] + 1);
}
if (j + 1 <= tC) { // ์ฝ๋ฉ๋ ฅ +1 ๊ฒฝ์ฐ
dp[i][j + 1] = Math.min(dp[i][j + 1], dp[i][j] + 1);
}
// ๋ฌธ์ ๋ฅผ ํ์ด์ ๋ฅ๋ ฅ์น ์ฌ๋ฆฌ๊ธฐ
for (int[] problem : problems) {
int alp_req = problem[0];
int cop_req = problem[1];
int alp_rw = problem[2];
int cop_rw = problem[3];
int cost = problem[4];
if (i >= alp_req && j >= cop_req) {
int nA = Math.min(tA, i + alp_rw);
int nC = Math.min(tC, j + cop_rw);
dp[nA][nC] = Math.min(dp[nA][nC], dp[i][j] + cost);
}
}
}
}
return dp[tA][tC];
}
}