๐ป ํ๋ก๊ทธ๋๋จธ์ค : ๊ณผ์ผ ์ฅ์ - ๋ฌธ์ ๋งํฌ
import Foundation
func solution(_ k: Int, _ m: Int, _ score: [Int]) -> Int {
var newScore = score
var cost = 0
newScore = newScore.filter { $0 <= k }.sorted(by: >)
let boxCount = newScore.count / m
for i in 0..<boxCount {
let boxMinValue = newScore[(i * m) + (m - 1)]
cost += boxMinValue * m
}
return cost
}
import Foundation
func solution(_ k:Int, _ m:Int, _ score:[Int]) -> Int {
let s = score.sorted(by: >)
return stride(from: m-1, to: score.count, by: m)
.reduce(0) { $0 + s[$1] * m }
}
์ผ์ ํ ๊ฐ๊ฒฉ์ ๋๊ณ ์ํ์ค๋ฅผ ์์ฑํ๋ ๋ฐ ์ฌ์ฉ
์์๊ฐ๋ถํฐ ๋๊ฐ๊น์ง ์ผ์ ํ๊ฒ ์ฆ๊ฐํ๊ฑฐ๋ ๊ฐ์
stride() ํจ์ ๊ธฐ๋ณธ ๊ตฌ์กฐ
- stride(from: ์์๊ฐ, to: ๋๊ฐ(ํฌํจX), by: ๊ฐ๊ฒฉ)
- stride(from: ์์๊ฐ, through: ๋๊ฐ(ํฌํจO), by: ๊ฐ๊ฒฉ)
let array1 = Array(stride(from: 1, to: 10, by: 2))
print(array1) // ์ถ๋ ฅ: [1, 3, 5, 7, 9]
python list comprehension๊ฐ์๊ฒ ํน์ ์๋ ํ๊ณ ์ฒ์์ ์ฐพ์์๋๋ฐ, ๋น์ทํ๊ฑด ์ฐพ์ ๊ฒ ๊ฐ๋ค. ๋ ํธํ ์ฌ๋ฌ ๋ฉ์๋๋ค์ ์ฐพ๊ณ ์ตํ๊ณ ์ถ๋ค.
์์ผ๋ก ๋ค์ํ ๋ฐฉ๋ฒ์ผ๋ก ํ์ฉํ ์ ์์ ๊ฒ ๊ฐ๋ค.