๋ฐฑ์ค€ - ๋ฐฐ(Swift)

2022. 11. 7. 09:55ใ†Algorithm

๋ฐฑ์ค€ - ๋ฐฐ(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

https://www.acmicpc.net/problem/1092

 

1092๋ฒˆ: ๋ฐฐ

์ฒซ์งธ ์ค„์— N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๊ฐ ํฌ๋ ˆ์ธ์˜ ๋ฌด๊ฒŒ ์ œํ•œ์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๊ฐ’์€ 1,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ์…‹์งธ ์ค„์—๋Š” ๋ฐ•์Šค์˜ ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. M์€ 10,000๋ณด

www.acmicpc.net

 

 ์ฒ˜์Œ ๋‚˜์˜ ํ’€์ด

 

์ฒ˜์Œ์—๋Š” ํฌ๋ ˆ์ธ์„ ๊ณ„์†ํ•ด์„œ for๋ฌธ์„ ๋Œ๋ฉด์„œ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ•์Šค๋ฅผ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋‹ค๋ฉด ์˜ฎ๊ธฐ๊ณ  ์ฆ๊ฐ€์‹œํ‚ค๋Š” ๋ฐฉ์‹์œผ๋กœ ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ๋ฐฉ์‹์€ ํ†ต๊ณผํ•œ ํ’€์ด์™€ ๋™์ผํ•œ ๋กœ์ง์„ ๋”ฐ๋ฅธ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. (ํ˜น์‹œ ์ด์œ ๋ฅผ ์•Œ๊ณ  ๊ณ„์‹œ๋ฉด ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค)

 

import Foundation

let n = Int(readLine()!)!
var crane = readLine()!.components(separatedBy:" ").map{Int($0)!}
let m = Int(readLine()!)!
var arr = readLine()!.components(separatedBy:" ").map{Int($0)!}

crane.sort(by: >)
arr.sort(by: >)
var idx = 0
var cnt = 0

if crane[0] < arr[0] {
    print("-1")
    exit(0)
}

while true {
    if idx == arr.count {
        break
    }
    for x in crane {
        if x >= arr[idx] {
            idx += 1
        }
        if idx == arr.count {
            break
        }
    }
    cnt += 1
}

print(cnt)

 

 

 ๋ณ€๊ฒฝํ•œ ๋‚˜์˜ ํ’€์ด

 

import Foundation

let n = Int(readLine()!)!
var crane = readLine()!.components(separatedBy:" ").map{Int($0)!}
let m = Int(readLine()!)!
var arr = readLine()!.components(separatedBy:" ").map{Int($0)!}

crane.sort(by: >)
arr.sort(by: >)
var idx = 0
var cnt = 0

if crane[0] < arr[0] {
    print("-1")
    exit(0)
}

while true {
    if arr.isEmpty { // ๋ฐ•์Šค๋ฅผ ๋‹ค ์˜ฎ๊ฒผ๋‹ค๋ฉด
        break
    }
    for x in crane {
        for (i, ele) in arr.enumerated() {
            if x >= ele { // ํฌ๋ ˆ์ธ์ด ๋ฐ•์Šค๋ฅผ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋‹ค๋ฉด
                arr.remove(at: i)
                break
            }
        }
    }
    cnt += 1
}

print(cnt)