• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

피부과 문제 질문입니다.

23.07.15 21:42 작성 23.07.15 21:52 수정 조회수 157

0

강의 해설 잘 보았습니다!

저는 강사님 코드랑 다르게 풀어보았는데 이렇게 풀면 시간초과 문제가 있을까요??

public class 피부과 {
    static int getTime(String time){
        int H = Integer.parseInt(time.split(":")[0]) * 60;
        int M = Integer.parseInt(time.split(":")[1]);
        return H+M;
    }
    public static int solution(int[] laser, String[] enter){

        int answer = 0;
        Queue<Integer> queue = new LinkedList<>();
        ArrayList<Integer> info = new ArrayList<>();
        for(String x : enter){
            int t = getTime(x.split(" ")[0]);
            int n = Integer.parseInt(x.split(" ")[1]);
            info.add(laser[n]);
            queue.add(t);
        }
        int idx = 0;
        while(!queue.isEmpty()){
            int temp = queue.poll() + info.get(idx);
            int res = 0;
            for(int x : queue){
                if(temp > x){
                    res++;
                }
                else break;
            }
            answer = Math.max(answer,res);
            idx++;
        }

        return answer;
    }

    public static void main(String[] args){
        System.out.println(solution(new int[]{30, 20, 25, 15}, new String[]{"10:23 0", "10:40 3", "10:42 2", "10:52 3", "11:10 2"}));
        System.out.println(solution(new int[]{30, 20, 25, 15}, new String[]{"10:23 0", "10:40 3", "10:42 2", "10:52 3", "15:10 0", "15:20 3", "15:22 1", "15:23 0", "15:25 0"}));
        System.out.println(solution(new int[]{30, 20, 25, 15}, new String[]{"10:20 1", "10:40 1", "11:00 1", "11:20 1", "11:40 1"}));
    }
}

 

답변 1

답변을 작성해보세요.

0

안녕하세요^^

enter의 길이가 100,000이 입력되면 큐의 길이도 100,000이 되고 큐에서 하나 꺼낼때마사 for문으로 대기하는 사람을 카운팅하는게 좀 걸리는 부분이네요. 하지만 아이디어가 좋습니다. 잘 하셨습니다. 영상에서 제가 하는 방법도 꼭 익혀두시면 좋겠습니다.