ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [1차] 셔틀버스 Java 풀이 [프로그래머스]
    CodeingTestPrac/Java Coding Test 2023. 8. 29. 17:03

    https://school.programmers.co.kr/learn/courses/30/lessons/17678?language=java 

     

    프로그래머스

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

    programmers.co.kr

    구현 문제이다 . 상황이 복잡하였지만 , 카카오 코테 성격상 예시테케를 다맞추면 통과한다라는 믿음을 가지고 진행을 하자.

     

    처음 문제를 푸는데 있어  예시테케를 하나씩 통과하는 방식으로 구현을 하다보니  복잡성이 커져 수정이 불가했다.

    용기를 가지고 주요 로직 부분을 지우고 종이에 예시를 적으면서 출력되는 상황을 체크했다.

     

    문제 이해의 부분은 "버스는 여석이 있어도 바로 출발" 한다 라는 것이다.

     

    주어진  테케 2 번을 보면 알수 있다.

    -> 9시 에 출발하는 버스는 9시에 도착한 사람을 태우고 여석이 존재하는 만큼 태우고 바로 출발한다.

    -> 마지막 버스 9시 10분 버스에 탈 사람이 대기줄에 2 명이 있으니 콘은 이들중 마지막 사람보다 빨리 와야한다.

     

    다음 두가지만 순서대로 체크를 하면 된다. = 알고리즘

     

    1. 버스가 마지막인지 

     

    2. 1번을 만족 할때 버스의 여석이 존재하는지

     

    버스가 마지막이다

    ->버스에 여석이 있다.

    -> 마지막 버스시간에 타자 .(콘은 대기줄에서 동시간대에 마지막으로 서있는게 조건이다.)

     

    버스가 마지막이다

    ->버스에 여석이 없다.

    -> 마지막 버스에 탄 마지막 사람보다 1 분 빨리 오자.

     

    버스가 마지막이 아니다.

    -> 반복문을 진행한다.

     

    위의 로직을 생각하며 다음 주어진 3 개의 테스트를 하나씩 생각하면 전부 만족한다. 

    @Test
        public void suttle_test1(){
            int n = 1, t = 1, m = 5 ;
            String[] timetable = {"08:00", "08:01", "08:02", "08:03"}; // 9 시에  운행 1 대 ,  1 분 간격으로 오고 , 총 5 명 탑승 , 즉 콘은 9시 정각에 탑승
            String an = solution(n,t,m,timetable);
            System.out.println(an); // 9:9
        }
        @Test
        public void suttle_test2(){
            int n = 2, t = 10, m = 2 ;
            String[] timetable = {"09:10", "09:09", "08:00"} ;  // if con arrive at 9 : 10 : late
            String an = solution(n,t,m,timetable);
            System.out.println(an); // 9:9
        }
        @Test
        public void suttle_test3(){
            int n = 2, t = 1, m = 2;
            String[] timetable = {"09:00", "09:00", "09:00", "09:00"};
            String an = solution(n,t,m,timetable);
            System.out.println(an); // 8:59
    
        }

     

     

    Solution

    public String solution(int n, int t, int m, String[] timetable) {
    		// 배열 준비
            System.out.println("timeT : "+Arrays.toString(timetable));
            int[] times = new int[timetable.length];
            int indext = 0 ;
            for(String time : timetable){
                String [] timeItem = time.split(":");
                int h = Integer.parseInt(timeItem[0]);
                int min = Integer.parseInt(timeItem[1]);
                times[indext] = (h*60) + min ;
                indext++;
            }
            Arrays.sort(times);
            System.out.println("PeoplesTime : "+ Arrays.toString(times));
            int[] busS = new int[n];
    
            int baseT = 540 ; // 540 = 9*60
    
            for(int i = 0 ; i < n ;i++) {
                busS[i] = baseT + i*(t);
            }
    
            System.out.println("Bus S :"+Arrays.toString(busS));
    
    		// 정답 준비 + 로직 구현
            String answer = "";
            int currPindex = 0 ;
    
            boolean rideOnBTime = false;
            boolean rideOnLast = false;
    
            for(int b = 0 ; b < busS.length;b++) {
                int currB = busS[b] ;
                int left = m; 
                int too = currPindex + m ;
                for(int p = currPindex ;p < too;p++) {
                    if(p>times.length-1) continue;
                    // bus time
                    if(times[p] <= currB) {
                        currPindex++;
                        left--;
                    }
                }
                if(b == busS.length-1) {
                    if(left > 0){
                        rideOnBTime = true;
                    }else{
                        rideOnLast = true;
                    }
                }
            }
    
            int ah =0 ;
            int am = 0;
            int time = 0;
            if(rideOnBTime){
                time = busS[busS.length-1];
            }
            if(rideOnLast){
                time = times[currPindex - 1];
            }
            ah = time / 60;
            am = time % 60 ;
            // 1minture fast to ride
            if(rideOnLast){
                if(am == 0){
                    ah--;
                    am = 59;
                }else {
                    am--;
                }
    
            }
            String reh ="";
            String rem ="";
    
            if(ah < 10){
                reh ="0"+ah;
            }else {
                reh = Integer.toString(ah);
            }
    
            if(am < 10){
                rem ="0"+am;
            }else {
                rem = Integer.toString(am);
            }
            answer = reh+":"+rem;
            return answer;
        }
Designed by Tistory.