Skip to content

Commit 4cea76b

Browse files
authored
Merge pull request #899 from AlgorithmWithGod/suyeun84
[20250915] PGM / LV2 / 과제 진행하기 / 김수연
2 parents 7a67e56 + d3fe1c7 commit 4cea76b

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
```java
2+
import java.util.*;
3+
class Solution {
4+
static int toInt(String num) {return Integer.parseInt(num);}
5+
public String[] solution(String[][] plans) {
6+
int len = plans.length;
7+
ArrayList<String> answer = new ArrayList<>();
8+
Stack<Work> stack = new Stack<>();
9+
Arrays.sort(plans, (a, b) -> {
10+
if (a[1].compareTo(b[1]) == 0) return a[2].compareTo(b[2]);
11+
return a[1].compareTo(b[1]);
12+
});
13+
for (int i = 0; i < len-1; i++) {
14+
String work = plans[i][0];
15+
int cEndTime = toMinutes(plans[i][1]) + Integer.parseInt(plans[i][2]);
16+
int nStartTime = toMinutes(plans[i + 1][1]);
17+
18+
if (cEndTime > nStartTime) {
19+
stack.push(new Work(work, cEndTime - nStartTime));
20+
} else {
21+
int leftTime = nStartTime - cEndTime;
22+
answer.add(work);
23+
while (leftTime > 0 && !stack.isEmpty()) {
24+
Work w = stack.pop();
25+
if (w.time > leftTime) {
26+
stack.push(new Work(w.work, w.time - leftTime));
27+
break;
28+
} else if (w.time == leftTime) {
29+
answer.add(w.work);
30+
break;
31+
} else {
32+
leftTime -= w.time;
33+
answer.add(w.work);
34+
}
35+
}
36+
}
37+
}
38+
answer.add(plans[len-1][0]);
39+
while(!stack.isEmpty()) {
40+
answer.add(stack.pop().work);
41+
}
42+
return answer.toArray(new String[0]);
43+
}
44+
45+
private int toMinutes(String time) {
46+
String[] parts = time.split(":");
47+
return Integer.parseInt(parts[0]) * 60 + Integer.parseInt(parts[1]);
48+
}
49+
50+
static class Work {
51+
String work;
52+
int time;
53+
public Work(String work, int time) {
54+
this.work = work;
55+
this.time = time;
56+
}
57+
}
58+
}
59+
```

0 commit comments

Comments
 (0)