ABOUT ME

Exponentially Growing Developers

Today
Yesterday
Total
  • [Algorithm/Java] kakao 2021 광고 삽입
    알고리즘 2021. 7. 31. 04:36
    반응형
    
    class Solution {
        public int getTimesecondsByString(String time){
            String[] hms = time.split(":");
            int timeseconds = Integer.parseInt(hms[0])*3600
                                + Integer.parseInt(hms[1])*60
                                + Integer.parseInt(hms[2]);
    
            return timeseconds;
        }
    
        public String getStringByTimeseconds(int time){
            int h = time/3600;
            int m = (time%3600) / 60;
            int s = time%60;
    
            String hour = h<10? "0"+h:String.valueOf(h);
            String min = m==60? "00": m<10? "0"+m: String.valueOf(m);
            String sec = s==60? "00": s<10? "0"+s: String.valueOf(s);
            return hour+":"+min+":"+sec;
        }
        public String solution(String play_time, String adv_time, String[] logs) {
    
            int time[]= new int[360001];
            int playTimeSeconds = getTimesecondsByString(play_time);
            int advTimeSeconds = getTimesecondsByString(adv_time);
    
            for(String log : logs){
                String[] logStamp = log.split("-");
                int start = getTimesecondsByString(logStamp[0]);
                int end = getTimesecondsByString(logStamp[1]);
    
                for(int i=start; i<end; i++){
                    time[i]++;
                }
            }
    
            int result = 0;
            long max = 0;
            long sum=0;
    
            for(int i=0; i<advTimeSeconds; i++){
                max+=time[i];
            }
            sum=max;
            for(int i=advTimeSeconds; i<playTimeSeconds; i++){
    
                sum-=time[i-advTimeSeconds];
                sum+=time[i];
                if(max<sum){
                    max=sum;
                    result=i-advTimeSeconds+1;
                }
            }
    
    
            String answer = getStringByTimeseconds(result);
            return answer;
        }
    }
    

    이 문제는 코테때 복잡~~하게 건들다가 못풀던 기억이 났는데..
    다시 풀어보려니까 생각이 안나서 결국 힌트를 봤더니 시간을 초로 변경해서 배열에 담고 슬라이딩 윈도우로 읽어내는 거였다!
    알고나니 참 신기한..
    다음에 시간 문제 나오면 이런 접근법을 생각해내봐야겠다.

    댓글

Designed by Tistory.