CS/알고리즘_KAKAO BLIND RECRUITMENT

2018 KAKAO BLIND RECRUITMENT : [1차] 셔틀버스

Jedy_Kim 2021. 10. 5. 21:47
728x90

https://programmers.co.kr/learn/courses/30/lessons/17678

 

코딩테스트 연습 - [1차] 셔틀버스

10 60 45 ["23:59","23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59"] "18:00"

programmers.co.kr

 

// 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.*;
 
class Solution {
    public String solution(int n, int t, int m, String[] timetable) {
        
        //크루가 도착하는 시간을 오름차순으로 정렬
        PriorityQueue<Integer> crew = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer num1, Integer num2) {
                return num1-num2;
            }
        });
        
        // 계산의 편의를 위해 분으로 처리한다.
        for(String str : timetable) {
            crew.add(convert(str));
        }
        
        // 버스 도착시간
        int busStart = 540;
        int last = 0;
        while(n-->0) {
            
            // 수용가능 인원
            int capable = m;
            // 마지막으로 탄 크루의 시간
            int lastCrewTime = 0;
            
            while(!crew.isEmpty()) {
                // 현재 버스의 도착시간보다 일찍왔으며, 수용인원도 남아있을 때
                if(crew.peek() <= busStart && capable > 0) {
                    capable--;
                    lastCrewTime = crew.poll();
                } 
                else break;                
            }
            
            // 마지막 버스가 아닌경우
            if(n > 0) {
                // 기다리는 크루가 없다면
                if(crew.isEmpty()) {
                    // 마지막 버스 도착 시간
                    last = busStart + ((n+1* t);
                    break;
                }
                // 버스의 다음 시간 
                busStart += t; 
            }
            // 마지막 버스인 경우
            else {
                // 수용이 가능한 경우
                if(capable > 0) last = busStart;
                // 수용이 불가능 하다면, 마지막 크루원보다 1분 빨리
                else last = lastCrewTime - 1;
                break;
            }
            
        } 
         
        return String.format("%02d:%02d", last/60, last%60);
    }
    
    public int convert(String str) {        
        int[] parse = Arrays.stream(str.split(":")).mapToInt(Integer::parseInt).toArray();
        return parse[0]*60 + parse[1];        
    }
}
cs

// 복습

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.*;
 
class Solution {
    
    int convertTimeToMinute(String str) {
        int[] parse = Arrays.stream(str.split(":")).mapToInt(Integer::parseInt).toArray();
        return (parse[0]*60 + parse[1]);
    } 
    
    public String solution(int n, int t, int m, String[] timetable) {
        
        // 크루들의 시간을 오름차순으로 정렬하여 보관한다.
        PriorityQueue<Integer> crew = new PriorityQueue<>((x, y) -> Integer.compare(x, y));
        
        // 계산의 편의를 위해 크루들의 시간을 모두 분으로 환산한다.
        for(String crewTime : timetable) {
            crew.add(convertTimeToMinute(crewTime));
        }
        
        // 버스 도착시간
        int busTime = 540;
        // 최종적으로 콘의 버스탈 시간
        int last = 0;
        
        // n이 10일 경우 범위는 0 ~ 9 이므로, 계산에 이용할 때는 '+1'을 해줄 것. 
        while(n-->0) {
            
            // 수용 가능 인원 카운트
            int capable     = m; 
            // 마지막의 크루 시간
            int lastCrewTime = 0;
                        
            while(!crew.isEmpty()) {
                // 버스 도착시간 전 이미 도착한 크루들을 계산한다.
                if((crew.peek() <= busTime) && (capable > 0)) {
                    capable--;
                    lastCrewTime = crew.poll();
                } 
                else break;
            }
            
            // 버스 배차가 남았을 경우
            if(n > 0) {
                // 버스 배차가 남았지만 크루가 비어있는 경우
                if(crew.isEmpty()) {
                    last = busTime + ((n+1* t);
                    break;
                } 
                // 버스의 다음 도착 시간
                busTime += t;
            }
            // 버스 배차가 끝났을 경우
            else {
                // 수용이 가능한 경우
                if(capable > 0)  last = busTime; 
                // 수용이 불가능한 경우 
                else last = lastCrewTime - 1
                break;
            } 
        }        
         
        return String.format("%02d:%02d", last/60, last%60);
    }
}
cs

 

반응형