-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path코딩테스트공부.java
146 lines (116 loc) · 4.54 KB
/
코딩테스트공부.java
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.util.*;
public class Solution {
public int solution(int alp, int cop, int[][] problems) {
Set<Problem> problemSet = new HashSet<>();
problemSet.add(Problem.getAlgoUnitProblem());
problemSet.add(Problem.getCodeUnitProblem());
int algoTargetPower = 0;
int codeTargetPower = 0;
for (int[] problem : problems) {
algoTargetPower = Math.max(algoTargetPower, problem[0]);
codeTargetPower = Math.max(codeTargetPower, problem[1]);
if (!Problem.isExpensive(problem[2], problem[3], problem[4])) {
problemSet.add(new Problem(problem[0], problem[1], problem[2], problem[3], problem[4]));
}
}
int time = Integer.MAX_VALUE;
State initState = new State(alp, cop, 0);
PriorityQueue<State> pq = new PriorityQueue<>();
pq.add(initState);
Map<State, Integer> dp = new HashMap<>();
dp.put(initState, 0);
while (!pq.isEmpty()) {
State state = pq.remove();
if (state.isCodingExpert(algoTargetPower, codeTargetPower)) {
time = state.time;
break;
}
for (Problem problem : problemSet) {
State nextState = state.getNextState(problem.algoReward, problem.codeReward, problem.cost);
boolean isNextStateValid = (dp.containsKey(nextState) && nextState.time < dp.get(nextState)) || !dp.containsKey(nextState);
if (problem.isSolvable(state.algoPower, state.codePower) && isNextStateValid) {
pq.add(nextState);
dp.put(nextState, nextState.time);
}
}
}
return time;
}
private static class State implements Comparable<State> {
int algoPower;
int codePower;
int time;
public State(int algoPower, int codePower, int time) {
this.algoPower = algoPower;
this.codePower = codePower;
this.time = time;
}
public State getNextState(int algoPower, int codePower, int cost) {
return new State(this.algoPower + algoPower, this.codePower + codePower, this.time + cost);
}
public boolean isCodingExpert(int algoTargetPower, int codeTargetPower) {
return algoPower >= algoTargetPower && codePower >= codeTargetPower;
}
@Override
public int compareTo(State other) {
return this.time - other.time;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
State state = (State) o;
return algoPower == state.algoPower && codePower == state.codePower;
}
@Override
public int hashCode() {
return Objects.hash(algoPower, codePower);
}
}
private static class Problem {
int algoPower;
int codePower;
int algoReward;
int codeReward;
int cost;
public Problem(int algoPower, int codePower, int algoReward, int codeReward, int cost) {
this.algoPower = algoPower;
this.codePower = codePower;
this.algoReward = algoReward;
this.codeReward = codeReward;
this.cost = cost;
}
public static Problem getAlgoUnitProblem() {
return new Problem(0, 0, 1, 0, 1);
}
public static Problem getCodeUnitProblem() {
return new Problem(0, 0, 0, 1, 1);
}
public static boolean isExpensive(int algoReward, int codeReward, int cost) {
return algoReward + codeReward <= cost;
}
public boolean isSolvable(int algoPower, int codePower) {
return this.algoPower <= algoPower && this.codePower <= codePower;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Problem problem = (Problem) o;
return algoPower == problem.algoPower && codePower == problem.codePower && algoReward == problem.algoReward
&& codeReward == problem.codeReward && cost == problem.cost;
}
@Override
public int hashCode() {
return Objects.hash(algoPower, codePower, algoReward, codeReward, cost);
}
}
}