코딩테스트/카카오

카카오 1차 코딩테스트 4번 셔틀버스 문제풀이

글로벌디노 2018. 5. 31. 21:39

카카오 1차 코딩 테스트 4번 문제풀이

문제 및 해설 바로가기


4. 셔틀버스(난이도: 중)

n

t

m

timetable

answer

1

1

5

"08:00","08:01","08:02","08:03"

"09:00"

2

10

2

"09:10","09:09","08:00"

"09:09"

2

1

2

"09:00","09:00","09:00","09:00"

"08:59"

1

1

5

"00:01","00:01","00:01","00:01","00:01"

"00:00"

1

1

1

"23:59"

"09:00"

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"



C# 소스코드

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 1, t = 1, m = 5;
            string[] timeTable = { "08:00", "08:01", "08:02", "08:03" };
            List<int> timeList = new List<int>();
            List<int> busTimeList = new List<int>();

            busTimeList.Add(540);
            for(int i = 1; i < n; i++)
            {
                busTimeList.Add(busTimeList[i - 1] + t);
            }

            for (int i = 0; i < timeTable.Length; i++)
            {
                string[] strTemp = timeTable[i].Split(':');
                int time = 0, iTemp;
                if(int.TryParse(strTemp[0], out iTemp))
                {
                    time = iTemp * 60;
                }
                if(int.TryParse(strTemp[1], out iTemp))
                {
                    time += iTemp;
                }
                timeList.Add(time);
            }

            timeList.Sort();

            int idx = 0;
            int resultTime = 0;
            for(int i = 0; i < busTimeList.Count; i++)
            {
                if(i == busTimeList.Count - 1)
                {
                    resultTime = busTimeList[i];
                }

                int mCount = m;
                for(int j = idx; j < timeList.Count; j++)
                {
                    if(busTimeList[i] >= timeList[idx])
                    {
                        mCount--;
                        if (mCount == 0)
                        {
                            if(i == busTimeList.Count - 1)
                            {
                                resultTime = timeList[idx] - 1;
                            }
                            break;
                        }
                        idx++;
                    }
                }
            }

            Console.WriteLine("{0:D2}:{1:D2}", resultTime / 60, resultTime % 60);
        }
    }
}