Closed zekexros closed 3 years ago
삼각형의 가장자리를 제외하고, 합이 더 큰 값(max sum)을 저장한다.
import Foundation
let n = readLine()!
var array = [[Int]]()
for _ in 0..<Int(n)! {
let str = readLine()!
let arr = str.components(separatedBy: " ")
array.append(arr.map{ Int($0)! })
}
for i in 1..<array.count {
for j in 0..<array[i].count {
if j == 0 { // left
array[i][j] = array[i][j] + array[i - 1][j]
} else if j == array[i].count - 1 { // right
array[i][j] = array[i][j] + array[i - 1][j - 1]
} else {
array[i][j] = array[i][j] + max(array[i - 1][j - 1], array[i - 1][j])
}
}
}
print(array[array.count - 1].max()!)
https://www.acmicpc.net/problem/1932