https://school.programmers.co.kr/learn/courses/30/lessons/176962
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
- 과제는 시작하기로 한 시각이 되면 시작합니다.
- 새로운 과제를 시작할 시각이 되었을 때, 기존에 진행 중이던 과제가 있다면 진행 중이던 과제를 멈추고 새로운 과제를 시작합니다.
- 진행 중이던 과제를 끝냈을 때, 잠시 멈춘 과제가 있다면, 멈춰둔 과제를 이어서 진행합니다.
- 만약, 과제를 끝낸 시각에 새로 시작해야 되는 과제와 잠시 멈춰둔 과제가 모두 있다면, 새로 시작해야 하는 과제부터 진행합니다.
- 멈춰둔 과제가 여러 개일 경우, 가장 최근에 멈춘 과제부터 시작합니다.
풀이는 다음과 같다.
시간 : 분을 분으로 통합하고, 분을 기준으로 정렬을 해준다.
for (int i = 0; i < plans.length; i++) {
String[] hourMinute = plans[i][1].split(":");
plans[i][1] = String.valueOf(Integer.parseInt(hourMinute[0])*60
+ Integer.parseInt(hourMinute[1]));
}
for (int i = 0; i < plans.length; i++) {
for (int j = 0; j < plans.length; j++) {
if (Integer.parseInt(plans[i][1]) < Integer.parseInt(plans[j][1])) {
String[] temp = plans[i];
plans[i] = plans[j];
plans[j] = temp;
}
}
}
그 뒤에 스택을 이용하여 1~plans.length까지 i의 과제 시작 시간과 i-1의 시작 시작을 빼주어 남은 과제 시간을 구해주고 과제를 하고도 시간이 남은 경우 밀린 과제(스택)를 탐색하여 값의 변동을 주었다.
Stack<String> stringList = new Stack<>();
Stack<Integer> integerList = new Stack<>();
for (int i = 1; i < plans.length; i++) {
int remain = Integer.parseInt(plans[i][1]) - Integer.parseInt(plans[i - 1][1]);
if (Integer.parseInt(plans[i - 1][2]) - remain > 0) {
stringList.add(plans[i - 1][0]);
integerList.add(Integer.parseInt(plans[i - 1][2]) - remain);
} else if (Integer.parseInt(plans[i - 1][2]) <= remain) {
remain = remain - Integer.parseInt(plans[i - 1][2]);
ans.add(plans[i - 1][0]);
while (remain != 0) {
if (!integerList.isEmpty() && integerList.peek() - remain <= 0) {
ans.add(stringList.pop());
remain = -(integerList.pop() - remain);
} else if (!integerList.isEmpty() && integerList.peek() - remain > 0) {
integerList.add(integerList.pop() - remain);
remain = 0;
} else {
remain = 0;
}
}
}
if (i == plans.length - 1) {
ans.add(plans[i][0]);
}
}
마지막 과제까지 수행했다면 남은 과제를 최근 미룬 순서로 정답에 추가하였다.
if (!stringList.isEmpty() && !integerList.isEmpty()) {
int size = stringList.size();
for (int i = 0; i < size; i++) {
ans.add(stringList.pop());
integerList.pop();
}
}
이렇게 과제 진행하기 문제를 풀었다!
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
class Solution {
public String[] solution(String[][] plans) {
String[] answer = {};
ArrayList<String> ans = new ArrayList<>();
for (int i = 0; i < plans.length; i++) {
String[] hourMinute = plans[i][1].split(":");
plans[i][1] = String.valueOf(Integer.parseInt(hourMinute[0])*60
+ Integer.parseInt(hourMinute[1]));
}
for (int i = 0; i < plans.length; i++) {
for (int j = 0; j < plans.length; j++) {
if (Integer.parseInt(plans[i][1]) < Integer.parseInt(plans[j][1])) {
String[] temp = plans[i];
plans[i] = plans[j];
plans[j] = temp;
}
}
}
Stack<String> stringList = new Stack<>();
Stack<Integer> integerList = new Stack<>();
for (int i = 1; i < plans.length; i++) {
int remain = Integer.parseInt(plans[i][1]) - Integer.parseInt(plans[i - 1][1]);
if (Integer.parseInt(plans[i - 1][2]) - remain > 0) {
stringList.add(plans[i - 1][0]);
integerList.add(Integer.parseInt(plans[i - 1][2]) - remain);
} else if (Integer.parseInt(plans[i - 1][2]) <= remain) {
remain = remain - Integer.parseInt(plans[i - 1][2]);
ans.add(plans[i - 1][0]);
while (remain != 0) {
if (!integerList.isEmpty() && integerList.peek() - remain <= 0) {
ans.add(stringList.pop());
remain = -(integerList.pop() - remain);
} else if (!integerList.isEmpty() && integerList.peek() - remain > 0) {
integerList.add(integerList.pop() - remain);
remain = 0;
} else {
remain = 0;
}
}
}
if (i == plans.length - 1) {
ans.add(plans[i][0]);
}
}
if (!stringList.isEmpty() && !integerList.isEmpty()) {
int size = stringList.size();
for (int i = 0; i < size; i++) {
ans.add(stringList.pop());
integerList.pop();
}
}
answer = ans.toArray(new String[0]);
return answer;
}
}
'Programmers' 카테고리의 다른 글
[프로그래머스][JAVA]Lv. 2 - 연속된 부분 수열의 합 (0) | 2023.04.08 |
---|---|
[프로그래머스][JAVA]Lv. 2 - 방금그곡 (0) | 2023.04.07 |
[프로그래머스][JAVA]Lv. 2 - 가장 큰 수 (0) | 2023.03.29 |
[프로그래머스][JAVA]Lv. 2 - 전화번호 목록 (0) | 2023.03.27 |
[프로그래머스][JAVA]Lv. 2 - 캐시 (0) | 2023.03.27 |