πŸ‘©πŸ»‍πŸ’» iOSun
article thumbnail

문제 μ„€λͺ…

 

λͺ‡κ°œμ˜ 객싀이 ν•„μš”ν•œμ§€ κ΅¬ν•˜λŠ” 문제

ν•œ 객싀을 μ΄μ–΄μ„œ μ‚¬μš©ν•˜λ €λ©΄ μ²­μ†Œμ‹œκ°„ 10뢄을 더해야 ν•œλ‹€.

 

문제 풀이

1. μ˜ˆμ•½ μ‹œμž‘ μ‹œκ°„ κΈ°μ€€μœΌλ‘œ μ •λ ¬ν•œλ‹€.

2. μ‹œκ°„μ„ λΆ„μœΌλ‘œ κ³„μ‚°ν•œλ‹€. 10:10 -> 10*60 + 10 = 610 

3. 객싀 번호λ₯Ό λ°°μ •ν•˜κ³  λ§Œμ•½ λ°°μ •λœ μ˜ˆμ•½μ‹œκ°„μ˜ λλ‚˜λŠ” μ‹œκ°„+10보닀 μ˜ˆμ•½ μ‹œμž‘μ‹œκ°„μ΄ λ‚˜μ€‘μ΄λ©΄ κ·Έ 객싀을 이어 μ‚¬μš©ν•œλ‹€.

 

 

import Foundation

func solution(_ book_time:[[String]]) -> Int {
    var answer = 0
    let bookTime = book_time.sorted(by: {$0[0] < $1[0]})
    var bookNum = Array(repeating: 0, count: bookTime.count) // κ°μ‹€λ²ˆν˜Έ ν• λ‹Ή
   
    let bookTimeToMinutes = timeToMinutes(bookTime)
    var endTime = 0
    
    for i in 0..<bookTimeToMinutes.count {
        // κ°μ‹€λ²ˆν˜Έκ°€ ν• λ‹Ήλ˜μ§€ μ•Šμ•˜μœΌλ©΄
        if bookNum[i] == 0 {
            answer += 1
            bookNum[i] = answer
            endTime = bookTimeToMinutes[i][1]+10
        }
        // 이미 ν• λ‹Ήλœ 객싀이면 패슀
        else {
            continue
        }
        for j in i+1..<bookTimeToMinutes.count {
            // κ·Έ μ˜ˆμ•½μ˜ 객싀이 ν• λ‹Ήλ˜μ§€ μ•Šμ•˜κ³  && ν˜„μž¬ 객싀이 λλ‚˜λŠ” μ‹œκ°„ 이후에 μ‚¬μš©ν•  수 μžˆλ‹€λ©΄
            if bookNum[j] == 0 && endTime <= bookTimeToMinutes[j][0] {
                bookNum[j] = answer
                endTime = bookTimeToMinutes[j][1]+10
            }
        }
    }
    return answer
    
   
    
}

// λΆ„μœΌλ‘œ κ³„μ‚°ν•˜λŠ” λ©”μ†Œλ“œ ex) 10:10 -> 10*60 + 10 = 610
func timeToMinutes(_ bookTime: [[String]]) -> [[Int]] {
    var minutesArray = [[Int]]()
    for times in bookTime {
        var arr = [Int]()
        for time in times {
            let split = time.split(separator: ":").map {Int(String($0))!}
            arr.append(split[0]*60 + split[1])
        }
        minutesArray.append(arr)
    }
   return minutesArray
}

 

profile

πŸ‘©πŸ»‍πŸ’» iOSun

@iosun

ν¬μŠ€νŒ…μ΄ μ’‹μ•˜λ‹€λ©΄ "μ’‹μ•„μš”β€οΈ" λ˜λŠ” "κ΅¬λ…πŸ‘πŸ»" ν•΄μ£Όμ„Έμš”!

profile on loading

Loading...