https://school.programmers.co.kr/learn/courses/30/lessons/176962#
📝문제
📝풀이
과제 진행의 우선순위는 1. 시간 2. 최근에 멈춘 과제 이다.
1. 과제를 시간순으로 먼저 정렬해두고 하나씩 진행
2. 현재 과제를 딜레이 시키지 않고 처리할 수 있는 경우 처리
2-1. 밀린 과제가 있는 경우 밀린 과제 스택에서 하나를 꺼내서 다음 처리 과제 대상에 넣음
2-2. 밀린 과제가 없는 경우 다음 과제를 진행
3. 현재 과제를 딜레이 시켜야 할 경우는 현재 시간과 다음과제 시작시간을 비교해서 시간차이만큼 처리하고 스택에 다시 삽입.
4. 시간순 과제 리스트가 빌 경우 밀린 과제를 하나씩 꺼내서 처리
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
public class Solution{
private LinkedList<Plan> list = new LinkedList<>();
private Stack<Plan> delayPlans = new Stack<>();
private ArrayList<String> answer = new ArrayList<>();
public String[] solution(String[][] plans) {
list = Arrays.stream(plans)
.map(Plan::new)
.sorted((o1, o2) -> o1.time.compareTo(o2.time))
.collect(Collectors.toCollection(LinkedList::new));
LocalDateTime currentTime = null;
boolean delayedPlanProcFlag = false;
while (!list.isEmpty() || !delayPlans.isEmpty()) {
Plan plan = null;
if(delayedPlanProcFlag){
plan = delayPlans.pop();
delayedPlanProcFlag = false;
}else{
plan = list.poll();
currentTime = plan.time;
}
if(!list.isEmpty()){
Plan nextPlan = list.peek();
LocalDateTime estimatedTime = currentTime.plusMinutes(plan.duration);
int criteria = estimatedTime.compareTo(nextPlan.time);
if(criteria > 0){ // 현재 plan 의 duration 을 처리 못할 경우
delayPlans.add(plan.processPlanPartial(currentTime,nextPlan.time));
}else{
answer.add(plan.name);
if(criteria < 0 && !delayPlans.isEmpty()){
delayedPlanProcFlag = true;
currentTime = estimatedTime;
}
}
}else{
answer.add(plan.name);
while (!delayPlans.isEmpty()){
answer.add(delayPlans.pop().name);
}
}
}
return answer.toArray(String[]::new);
}
static class Plan{
String name;
LocalDateTime time;
int duration;
public Plan(String[] plan){
this.name = plan[0];
String[] splitTime = plan[1].split(":");
this.time = LocalDateTime.of(0, Month.JANUARY,1,Integer.parseInt(splitTime[0]), Integer.parseInt(splitTime[1]));
this.duration = Integer.parseInt(plan[2]);
}
public Plan processPlanPartial(LocalDateTime currentTime, LocalDateTime nextPlanTime){
long diff = Math.abs(ChronoUnit.MINUTES.between(nextPlanTime, currentTime));
this.duration -= diff;
return this;
}
}
}
'알고리즘,PS > 프로그래머스' 카테고리의 다른 글
[프로그래머스] [Level2] 가장큰수 JAVA (0) | 2023.10.10 |
---|---|
[프로그래머스] [Level2] 배달JAVA (0) | 2022.03.28 |
[프로그래머스] [Level2] 양궁대회 JAVA (0) | 2022.02.14 |
[프로그래머스] [Level3] 양과늑대 JAVA (0) | 2022.02.08 |
[프로그래머스] [Level2] 주차 요금 계산JAVA (0) | 2022.02.05 |