Skip to content

[Algorithm] 호텔 대실 #104

Description

@hwangJi-dev

💬 문제

[코딩테스트 연습 - 호텔 대실](https://school.programmers.co.kr/learn/courses/30/lessons/155651)


💬 Idea

  1. 예약 타임을 Int로 형변환해준다
    1. 15:00 → 1500
  2. 퇴실 시간에 청소시간 10분을 합쳐준다.
    • 이 때 퇴실 시간이 50분을 넘겼을 때를 유의해야 한다. (테스트케이스 9,17번 주의)
    • 퇴실 시간이 55분일 때 10분을 더해주게 되면 → 65분이 되므로
      • 청소시간을 합친 분 시간이 60분을 넘는다면 40을 더해줘야 한다.
  3. bookList를 돌며 이미 배정된 룸들을 같이 돌아주며 퇴실시간보다 입실 시간이 늦거나, 입실 시간보다 퇴실 시간이 빠른 경우 해당 룸에 예약 리스트를 추가한다.
    • 이 때 손님이 아무 방도 배정받지 못한다면 (이미 해당 시간에 예약이 찼다면) 객실을 하나 추가해준다.

💬 풀이

func solution(book_time:[[String]])->Int{varbookList:[(Int,Int)]=[]
// Int로 시간변환
forbin book_time {lett1=Int(b[0].components(separatedBy:":").map{String($0)}.joined())!
vart2=Int(b[1].components(separatedBy:":").map{String($0)}.joined())! +10
// 청소시간을 합친 분 시간이 60분을 넘는다면
if t2 %100>=60{
// 40을 더해준다
t2 +=40}
bookList.append((t1, t2))}
// 퇴실 시간이 큰 순서대로 정렬
bookList = bookList.sorted(by:{ $0.1> $1.1})varassignRoomList:[[(Int,Int)]]=[[bookList.first!]]forbin1..<bookList.count {varisBooked=falsefor(i, r)in assignRoomList.enumerated(){
// 퇴실시간보다 입실 시간이 늦거나, 입실 시간보다 퇴실 시간이 빠른 경우
ifbookList[b].0>= r.last!.1 || r.last!.0>=bookList[b].1{
// 해당 룸에 예약 리스트를 추가한다
assignRoomList[i].append(bookList[b])
isBooked =truebreak}}
// 예약되지 않았다면
if !isBooked {
// 새로운 방을 배정한다
assignRoomList.append([bookList[b]])}}return assignRoomList.count
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions