ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

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

[๋ฌธ์ œ ํ’€์ด]

์…”ํ‹€ ๋ฒ„์Šค๋ฅผ nํšŒ ์šดํ–‰ํ•˜๋ฉฐ, ๋Œ€๊ธฐ์ค„์— ์žˆ๋Š” ํƒ‘์Šน ๊ฐ€๋Šฅํ•œ ์‚ฌ๋žŒ์„ ์ฐจ๋ก€๋Œ€๋กœ ํƒœ์›๋‹ˆ๋‹ค.

๋งˆ์ง€๋ง‰ ์…”ํ‹€๋ฒ„์Šค๊ฐ€ ์šดํ–‰๋ ๋•Œ, ๋Œ€๊ธฐ์ค„์ด m๋ณด๋‹ค ์ž‘์œผ๋ฉด, ๋งˆ์ง€๋ง‰ ์…”ํ‹€ ์‹œ๊ฐ„์„ answer์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

๋˜ํ•œ, ๋Œ€๊ธฐ์ค„์ด m๋ณด๋‹ค ํฌ๋‹ค๋ฉด, ๋Œ€๊ธฐ์ค„์˜ ๋งˆ์ง€๋ง‰ ์‚ฌ๋žŒ์˜ ์‹œ๊ฐ„์—์„œ 1๋ถ„์„ ๋บ€ ์‹œ๊ฐ„์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

- ์šฐ์„  ๋Œ€๊ธฐ์ค„์„ ์‹œ๊ฐ„์ˆœ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

- timetable[]์˜ ์‹œ๊ฐ„์„ int ํ˜•์œผ๋กœ ์‹œ๊ฐ„์„ ๋ถ„๋‹จ์œ„๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ intTimeTable[]์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

: ์‹œ์ž‘ ์‹œ๊ฐ„ time = 540 (9:00)

- ์…”ํ‹€๋ฒ„์Šค์˜ ์‹œ๊ฐ„ time์„ t๋งŒํผ ๋”ํ•ด ๊ฐฑ์‹ ํ•˜๋ฉฐ time ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๋Œ€๊ธฐ์‹œ๊ฐ„์— ์žˆ๋Š” ์‚ฌ๋žŒ๋“ค์„ ์ตœ๋Œ€ m๋งŒํผ ํƒœ์›๋‹ˆ๋‹ค.

- ๋งˆ์ง€๋ง‰ ์…”ํ‹€ ๋ฒ„์Šค ์‹œ๊ฐ„์ด ๋์„๋•Œ, last ๋ณ€์ˆ˜์— ๋งˆ์ง€๋ง‰ ๋Œ€๊ธฐ์ค„ ๊ธธ์ด๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

- ๋งŒ์•ฝ last < m ์ด๋ฉด ๋งˆ์ง€๋ง‰ ์…”ํ‹€์‹œ๊ฐ„์„ answer์— ์ €์žฅํ•˜๊ณ , last <= m ์ด๋ผ๋ฉด ๋Œ€๊ธฐ์ค„ ๋งˆ์ง€๋ง‰ ์‚ฌ๋žŒ์˜ ์‹œ๊ฐ„์—์„œ 1๋ถ„ ๋บ€ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

[์ •๋‹ต ์ฝ”๋“œ]

import java.util.*;
class Solution {
    // nํšŒ t๋ถ„ ๊ฐ„๊ฒฉ์œผ๋กœ ์—ญ์— ๋„์ฐฉํ•จ
    public static int[] intTimeTable;
    public String solution(int n, int t, int m, String[] timetable) {
        String answer = "";
        intTimeTable = new int[timetable.length];
        Arrays.sort(timetable);
        // intํ˜• ์‹œ๊ฐ„์œผ๋กœ ๋ณ€ํ™˜
        for(int i=0;i<timetable.length;i++) {
            String[] str = timetable[i].split(":");
            intTimeTable[i] = Integer.parseInt(str[0])*60 + Integer.parseInt(str[1]);
        }
        int index = 0;
        int time = 9*60; // ์…”ํ‹€ ์‹œ๊ฐ„
        int last = 0; // ๋งˆ์ง€๋ง‰ ๋Œ€๊ธฐ์ค„ ๊ธธ์ด
        
        // ๋งˆ์ง€๋ง‰ ๋ฒ„์Šค ๋นผ๊ณ  ์ „๋ถ€ ํƒœ์›€
        for(int i=0;i<n;i++) {
            for(int j=0;j<m;j++) {
                // ๋”์ด์ƒ ์‚ฌ๋žŒ์ด ์—†์Œ
                if(index > timetable.length - 1) {
                    break;
                }
                if(time >= intTimeTable[index]) {
                    index++;
                    // ๋งˆ์ง€๋ง‰ ์…”ํ‹€
                    if(i == n-1) {
                        last++;
                    }
                }
            }
            time += t;
        }
        time -= t;
        if(last < m) {
            answer = stringTime(time);
        } else {
            answer = stringTime(intTimeTable[index - 1] - 1);
        }
        return answer;
    }
    public static String stringTime(int intTime) {
        String str = "";
        int hour = intTime/60;
        if(hour < 10) {
            str += "0" + hour;
        } else {
            str += hour;
        }
        str += ":";
        int minute = intTime%60;
        if(minute < 10) {
            str += "0" + minute;
        } else {
            str += minute;
        }
        return str;
    }
}
/*
์ฐจ๊ฐ€ ์šดํ–‰์„ 10๋ฒˆ ํ•˜๋ฉด
์ฐจ๋ก€๋กœ ์‚ฌ๋žŒ์„ ํƒœ์›€

if) ๋งˆ์ง€๋ง‰ ์ฐจ ๋Œ€๊ธฐ์ค„ > m
-> ๋Œ€๊ธฐ์ค„ ๋งˆ์ง€๋ง‰ ์‚ฌ๋žŒ -1
else)
-> ๋งˆ์ง€๋ง‰ ์ฐจ ์‹œ๊ฐ„
*/