https://school.programmers.co.kr/learn/courses/30/lessons/176962
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
import java.util.*;
class Solution {
private static class Work{
String name;
int workingTime;
public Work(String name, int workingTime){
this.name = name;
this.workingTime = workingTime;
}
}
public String[] solution(String[][] plans) {
int size = plans.length;
String[] answer = new String[size];
Arrays.sort(plans,(o1,o2)->{
return o1[1].compareTo(o2[1]);
});
Stack<Work> stack = new Stack<>();
int prevTime = 0;
int answerIndex = 0;
for(int i = 0 ; i < size ; i++){
String[] plan = plans[i];
// System.out.println(Arrays.deepToString(plan));
String name = plan[0];
int startTime = stringToTime(plan[1]);
int workingTime = stringToTime(plan[2]);
while(!stack.empty()){
Work prevWork = stack.pop();
int endTime = prevTime + prevWork.workingTime;
if(endTime <= startTime){
answer[answerIndex++] = prevWork.name;
prevTime += prevWork.workingTime;
}else{
stack.push(new Work(prevWork.name, prevWork.workingTime - (startTime - prevTime)));
break;
}
}
stack.push(new Work(name,workingTime));
prevTime = startTime;
}
while(!stack.empty()){
answer[answerIndex++] = stack.pop().name;
}
return answer;
}
private static int stringToTime(String str){
String[] temp = str.split(":");
int result = 0;
if(temp.length == 2){
result = Integer.parseInt(temp[0]) * 60 + Integer.parseInt(temp[1]);
}else{
result = Integer.parseInt(str);
}
return result;
}
}
'알고리즘' 카테고리의 다른 글
백준 5566번 주사위 게임 (JAVA) (0) | 2023.04.05 |
---|---|
프로그래머스 호텔 대실 (JAVA) (0) | 2023.04.04 |
프로그래머스 표현 가능한 이진트리 (JAVA) (0) | 2023.04.02 |
프로그래머스 택배 배달과 수거하기(JAVA) (0) | 2023.04.01 |
프로그래머스 이모티콘 할인행사 (JAVA) (0) | 2023.03.31 |