-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDH_118668.java
More file actions
61 lines (48 loc) ยท 3.04 KB
/
DH_118668.java
File metadata and controls
61 lines (48 loc) ยท 3.04 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
55
56
57
58
59
60
61
import java.util.*;
public class DH_118668 {
static public int solution(int alp, int cop, int[][] problems) {
int answer = 0, maxAlpReq = 0, maxCopReq = 0;
for(int i = 0; i < problems.length; i++) {
maxAlpReq = Math.max(problems[i][0], maxAlpReq);
maxCopReq = Math.max(problems[i][1], maxCopReq);
}
// dp[r][c]: ์๊ณ ๋ ฅ์ด r์ด๊ณ , ์ฝ๋ฉ๋ ฅ์ด c๊ฐ ๋๊ธฐ ์ํด์ ๊ฑธ๋ฆฌ๋ ์ต์ ์๊ฐ
int[][] dp = new int[maxAlpReq + 1][maxCopReq + 1];
for(int r = 0; r < dp.length; r++) Arrays.fill(dp[r], Integer.MAX_VALUE);
// alp์ cop๊ฐ ์ด๋ฏธ ์ต๋๊ฐ์ ๋์์ ์๋ ์๊ธฐ ๋๋ฌธ์ ๊ฐ ์กฐ์
alp = Math.min(alp, maxAlpReq);
cop = Math.min(cop, maxCopReq);
dp[alp][cop] = 0;
for(int r = alp; r < dp.length; r++) {
for(int c = cop; c < dp[0].length; c++) {
// ์๊ณ ๋ฆฌ์ฆ์ ๊ณต๋ถํด์ ์๊ณ ๋ ฅ์ 1 ๋์ด๋ ๊ฒฝ์ฐ
if(r + 1 < dp.length) dp[r + 1][c] = Math.min(dp[r + 1][c], dp[r][c] + 1);
// ์ฝ๋ฉ ๊ณต๋ถ๋ฅผ ํด์ ์ฝ๋ฉ๋ ฅ์ 1 ๋์ด๋ ๊ฒฝ์ฐ
if(c + 1 < dp[0].length) dp[r][c + 1] = Math.min(dp[r][c + 1], dp[r][c] + 1);
// ํ ์ ์๋ ๋ฌธ์ ๋ฅผ ํ๋ ํ์ด ์๊ณ ๋ ฅ๊ณผ ์ฝ๋ฉ๋ ฅ์ ๋์ด๋ ๊ฒฝ์ฐ
for(int k = 0; k < problems.length; k++) {
int alpReq = problems[k][0]; // ๋ฌธ์ ๋ฅผ ํ๊ธฐ ์ํด ํ์ํ ์๊ณ ๋ ฅ
int copReq = problems[k][1]; // ๋ฌธ์ ๋ฅผ ํ๊ธฐ ์ํด ํ์ํ ์ฝ๋ฉ๋ ฅ
int alpRwd = problems[k][2]; // ๋ฌธ์ ๋ฅผ ํ์์ ๋ ์ฆ๊ฐํ๋ ์๊ณ ๋ ฅ
int copRwd = problems[k][3]; // ๋ฌธ์ ๋ฅผ ํ์์ ๋ ์ฆ๊ฐํ๋ ์ฝ๋ฉ๋ ฅ
int cost = problems[k][4]; //๋ฌธ์ ๋ฅผ ํธ๋๋ฐ ๋๋ ์๊ฐ
if(r < alpReq || c < copReq) continue; // ํ์ฌ ์๊ณ ๋ ฅ์ด๋ ์ฝ๋ฉ๋ ฅ์ด ๋ถ์กฑํ ๊ฒฝ์ฐ
// ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ ๊ฒฝ์ฐ continue๋ฅผ ํ๋ฉด ์ค๋ต!
// ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ฉด ๋ฌด์กฐ๊ฑด ๋ฌธ์ ๋ฅผ ํ ์ ์๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ maxAlpReq, maxCopReq๊ฐ ๋ ๊ฒ์ด๋ผ๊ณ ์๊ฐํด์ผ ๋จ
// if(dp.length < r + alpRwd || maxCopReq < c + copRwd) continue; // ๋ฒ์๋ฅผ ๋ฒ์ด๊ฐ๋ ๊ฒฝ์ฐ
// dp[r + alpRwd][c + copRwd] = Math.min(dp[r + alpRwd][c + copRwd], dp[r][c] + cost);
int nextAlp = Math.min(maxAlpReq, r + alpRwd);
int nextCop = Math.min(maxCopReq, c + copRwd);
dp[nextAlp][nextCop] = Math.min(dp[nextAlp][nextCop], dp[r][c] + cost);
}
}
}
answer = dp[maxAlpReq][maxCopReq];
return answer;
}
public static void main(String[] args) {
int alp = 10, cop = 10;
int[][] problems = {{10, 15, 2, 1, 2}, {20, 20, 3, 3, 4}};
System.out.println(solution(alp, cop, problems));
}
}