๋ฐฑ์ค - ๋ฐฐ(Swift)
2022. 11. 7. 09:55ใAlgorithm
๋ฐฑ์ค - ๋ฐฐ(Swift)
๋ฌธ์ ์ค๋ช
https://www.acmicpc.net/problem/1092
์ฒ์ ๋์ ํ์ด
์ฒ์์๋ ํฌ๋ ์ธ์ ๊ณ์ํด์ 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)
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค - ๊ณผ์ (Swift) (0) | 2022.11.07 |
---|---|
๋ฐฑ์ค - ์ด์ฅ๋ ์ด๋(Swift) (0) | 2022.11.07 |
์ํ ๋ - ๋ฐฑ์ค(Swift) (0) | 2022.11.06 |
๋ฐฑ์ค - UCPC๋ ๋ฌด์์ด ์ฝ์์ผ๊น?(Swift) (0) | 2022.11.06 |
๋ฐฑ์ค - A์ B(Swift) (0) | 2022.11.06 |