forked from GreatAlgorithm-Study/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYJ_118668.java
More file actions
91 lines (79 loc) · 2.98 KB
/
YJ_118668.java
File metadata and controls
91 lines (79 loc) · 2.98 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.*;
public class YJ_118668 {
public static void main(String[] args) {
int alp = 10;
int cop = 10;
int[][] problems = {{10,15,2,1,2},{20,20,3,3,4}};
System.out.println(solution(alp,cop,problems));
}
static class Problem {
int alpReq;
int copReq;
int alpRwd;
int copRwd;
int cost;
public Problem(int alpReq, int copReq, int alpRwd, int copRwd, int cost) {
this.alpReq = alpReq;
this.copReq = copReq;
this.alpRwd = alpRwd;
this.copRwd = copRwd;
this.cost = cost;
}
}
static List<Problem> list = new ArrayList<>();
static final int MAX_TIME = 300;
static int[][] dp;
static public int solution(int alp, int cop, int[][] problems) {
int maxAlp = 0;
int maxCop = 0;
for(int[] problem : problems){
list.add(new Problem(problem[0], problem[1], problem[2], problem[3], problem[4]));
maxAlp = Math.max(maxAlp,problem[0]);
maxCop = Math.max(maxCop,problem[1]);
}
//모든 문제 풀 수 있는 알고력,코딩력 충족
if(alp >= maxAlp && cop >= maxCop){
return 0;
}
dp = new int[maxAlp+2][maxCop+2];
for(int[] d : dp){
Arrays.fill(d,MAX_TIME);
}
//현재 알고력과 코딩력이 목표치 보다 높을 경우 보정하기
if(alp > maxAlp){
alp = maxAlp;
}
if(cop > maxCop){
cop = maxCop;
}
dp[alp][cop] = 0;
calculateDp(alp, cop, maxAlp, maxCop);
return dp[maxAlp][maxCop];
}
static void calculateDp(int alp, int cop, int maxAlp, int maxCop){
for(int i=alp; i<maxAlp+1; i++){
for(int j=cop; j<maxCop+1; j++){
dp[i+1][j] = Math.min(dp[i+1][j], dp[i][j]+1);
dp[i][j+1] = Math.min(dp[i][j+1], dp[i][j]+1);
for(Problem problem : list){
if(i < problem.alpReq || j < problem.copReq){
continue;
}
int solvedAlp = i + problem.alpRwd;
int solvedCop = j + problem.copRwd;
if(solvedAlp >= maxAlp && solvedCop >= maxCop){
dp[maxAlp][maxCop] = Math.min(dp[maxAlp][maxCop], dp[i][j]+problem.cost);
}
else if(solvedAlp >= maxAlp){
dp[maxAlp][solvedCop] = Math.min(dp[maxAlp][solvedCop], dp[i][j]+problem.cost);
}else if(solvedCop >= maxCop){
dp[solvedAlp][maxCop] = Math.min(dp[solvedAlp][maxCop], dp[i][j]+problem.cost);
}
else { //solvedAlp < maxAlp && solvedCop < maxCop
dp[solvedAlp][solvedCop] = Math.min(dp[solvedAlp][solvedCop], dp[i][j]+problem.cost);
}
}
}//j
}//i
}
}