코딩공부/프로그래머스

※[프로그래머스]Lv.2 호텔 대실 C#

usingsystem 2023. 6. 30. 17:36
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/155651

 

프로그래머스

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

programmers.co.kr

소스코드

시간을 다루는 예약 같은 경우 orderby를 통해 시간순으로 정렬하여 끝시간과 첫 시간만 비교한다.

using System;
using System.Collections.Generic;
using System.Linq;

public class Solution
{
    public int solution(string[,] book_time)
    {
        var roomList = new List<int>();
        List<Tuple<int, int>> timeList = new List<Tuple<int, int>>();

        for (int i = 0; i < book_time.GetLength(0); i++)
        {
            int[] temp;
            temp = Array.ConvertAll(book_time[i, 0].Split(":"), int.Parse);
            int start = (temp[0] * 60) + (temp[1]);

            temp = Array.ConvertAll(book_time[i, 1].Split(":"), int.Parse);
            int end = (temp[0] * 60) + temp[1] + 10;

            timeList.Add(new Tuple<int, int>(start, end));
        }

        timeList = timeList.OrderBy(o => o.Item1).ToList();
        roomList.Add(timeList[0].Item2);

        for (int i = 1; i < timeList.Count(); i++)
        {
            int roomIdx = roomList.FindIndex(f => f <= timeList[i].Item1);

            if (roomIdx == -1)
                roomList.Add(timeList[i].Item2);
            else
                roomList[roomIdx] = timeList[i].Item2;
        }
        return roomList.Count();
    }
}
728x90