알고리즘

프로그래머스 과제 진행하기 (JAVA)

박카스마시며코딩 2023. 4. 3. 14:55

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;
    }
}