본문 바로가기

알고리즘,PS/프로그래머스

[프로그래머스] [Level2] 과제진행하기 JAVA

https://school.programmers.co.kr/learn/courses/30/lessons/176962#

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

📝문제

📝풀이

과제 진행의 우선순위는 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;
        }
    }
}

 

Recent Posts
Popular Posts
Archives
Visits
Today
Yesterday