๋ฐ์ํ
๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/9465
ํ์ด IDEA
์ด๋ ๊ฒ ๋ฐ๊ฒฌํ ์ ํ์์ ์๋์ ๊ฐ๋ค. ์ฌ๊ธฐ์ dp ๋ ์์ ๊ทธ๋ฆผ์ผ๋ก ๋ํ๋ธ ์ต๋๊ฐ๋ค์ ์ ์ฅํ๋ ๋ฐฐ์ด์ด๊ณ , arr ๋ ์ฐ๋ฆฌ๊ฐ ์ ๋ ฅ๋ฐ์ ์คํฐ์ปค๋ค์ ๊ฐ๊ฒฉ์ด ๋ช ์๋์ด์๋ ๋ฐฐ์ด์ด๋ค.
dp[0][i] = max(dp[1][i-1] + arr[0][i], dp[1][i-2] + arr[0][i])
dp[1][i] = max(dp[0][i-1] + arr[1][i], dp[0][i-2] + arr[1][i])
ํด๋น ์ ํ์์ ํ์ฉํ์ฌ ๋ฌธ์ ๋ฅผ ํ๋ฉด ๋๋ค.
์์ค์ฝ๋ - ๋ง์์ต๋๋ค!
- ๋๋ sticker ๋ผ๋ ํจ์๋ฅผ ํตํด dp ๋ฅผ ๊ตฌ์ฑํ๋ค.
- ๊ทธ๋ฆฌ๊ณ for ๋ฌธ์ ์ด์ฉํด์ ์คํฐ์ปค๋ค์ ๋ฐฐ์ด์ ์ ๋ ฅ๋ฐ์๋ค.
let count = Int(readLine()!)!
for _ in 1...count {
let n = Int(readLine()!)!
var arr: [Array<Int>] = []
for _ in 0...1 {
let input = readLine()!.split(separator: " ").map { Int(String($0))! }
arr.append(input)
}
let result = sticker(n: n, arr: arr)
if n == 1 {
print(max(result[0][0], result[1][0]))
} else if n == 2 {
print(max(result[0][1], result[1][1]))
} else {
print(max(result[0][n-1], result[1][n-1]))
}
}
func sticker(n: Int, arr: Array<Array<Int>>) -> Array<Array<Int>> {
var dp = Array(repeating: Array(repeating: 0, count: n), count: 2)
dp[0][0] = arr[0][0]
dp[1][0] = arr[1][0]
if n > 1 {
dp[0][1] = arr[1][0] + arr[0][1]
dp[1][1] = arr[0][0] + arr[1][1]
for i in 2..<n {
dp[0][i] = max(dp[1][i-1] + arr[0][i], dp[1][i-2] + arr[0][i])
dp[1][i] = max(dp[0][i-1] + arr[1][i], dp[0][i-2] + arr[1][i])
}
}
return dp
}
๋ฐ์ํ