iOS 개발 공부

[Swift] 숫자 게임 본문

코딩테스트/프로그래머스

[Swift] 숫자 게임

물복딱복준복 2023. 6. 18. 21:09
import Foundation

func solution(_ a:[Int], _ b:[Int]) -> Int {
    
    var result = 0
    var a = a.sorted(by: <)
    var b = b.sorted(by: <)
    
    while !a.isEmpty && !b.isEmpty && a.first! < b.last! {
        if a.first! < b.removeFirst() {
            a.removeFirst()
            result += 1
        }
    }
    
    return result
}

func solution2(_ a:[Int], _ b:[Int]) -> Int {

    var index = 0
    let a = a.sorted()
    let b = b.sorted()

    for element in b {
        if a[index] < element {
            index += 1
        }
    }

    return index
}

 

'코딩테스트 > 프로그래머스' 카테고리의 다른 글

[Swift] 베스트앨범  (0) 2023.06.18
[Swift] 기지국 설치  (0) 2023.06.18
[Swift] 숫자 카드 나누기  (0) 2023.06.18
[Swift] 멀쩡한 사각형  (0) 2023.06.17
[Swift] 호텔 대실  (1) 2023.06.17