아래는 8ms 코드이고
let N: Int = Int(readLine()!)!
var stepScore: [Int] = [Int]()
for _ in 0..<N {
stepScore.append(Int(readLine()!)!)
}
var maxScore: [[Int]] = [[Int]]()
maxScore.append([stepScore[0], 0])
if N >= 2 {
maxScore.append([stepScore[0]+stepScore[1], stepScore[1]])
for i in 2..<N {
let oneStep = maxScore[i-1][1] + stepScore[i]
let twoStep = max(maxScore[i-2][0], maxScore[i-2][1]) + stepScore[i]
maxScore.append([oneStep, twoStep])
}
}
print(max(maxScore[N-1][0], maxScore[N-1][1]))
아래는 12ms 코드이다.
import Foundation
let n = Int(readLine()!)!
var A: [Int] = [Int]()
for _ in 0..<n {
A.append(Int(readLine()!)!)
}
var arr:[[Int]] = []
arr.append([A[0],0])
if n >= 2 {
arr.append([A[0]+A[1], 0 + A[1]])
for i in 2..<n {
let 전 = arr[i-1][1]+A[i]
let 전전 = max(arr[i-2][0] , arr[i-2][1]) + A[i]
arr.append([전, 전전])
}
}
print(max(arr[n-1][0], arr[n-1][1]))
같은데 시간이 다르다. 뭐지요..?