Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 1912๋ฒˆ - ์—ฐ์†ํ•ฉ (๊ฐ€์žฅ ๊ธฐ์ดˆ์ ์ธ DP ๋ฌธ์ œ)

๊ฐ์ž ๐Ÿฅ” 2022. 7. 7. 16:02
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/1912

 

1912๋ฒˆ: ์—ฐ์†ํ•ฉ

์ฒซ์งธ ์ค„์— ์ •์ˆ˜ n(1 ≤ n ≤ 100,000)์ด ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„์—๋Š” n๊ฐœ์˜ ์ •์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ์ˆ˜์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ˆ˜๋Š” -1,000๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜์ด๋‹ค.

www.acmicpc.net

 

๐ŸŸ  ๋ฌธ์ œ ํ’€์ด ์•„์ด๋””์–ด

๊ฐ€์žฅ ๊ธฐ์ดˆ์ ์ธ dp๋ฌธ์ œ๋กœ ์ด๋ฏธ ํ’€์–ด๋ดค๋˜ ๋ฌธ์ œ๋ผ ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ•˜์ง€ ์•Š์•˜๋‹ค.

dp ์—๋Š” ํ˜„์žฌ์œ„์น˜์˜idx-1 ๊นŒ์ง€์˜ ํ•ฉ๊ณผ ํ•ด๋‹น arr[idx] ๊ฐ’์„ ๋น„๊ตํ•ด๋ณธ ํ›„ ์ตœ๋Œ€๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค. ์•ž์—์„œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ฐจ๊ณก์ฐจ๊ณก ๋”ํ•ด์ง„ ๊ฐ’์— ํ˜„์žฌ๊ฐ’์„ ๋”ํ•˜๋Š”๊ฒŒ ๋” ํฐ์ง€, ํ˜„์žฌ๊ฐ’๋งŒ ๋ฐ”๋ผ๋ณด๋Š”๊ฒŒ ๋” ํฐ์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค! 

 

๐ŸŸ  ์ „์ฒด ์†Œ์Šค์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Boj/1912_%EC%97%B0%EC%86%8D%ED%95%A9/main.swift

 

GitHub - deslog/Algorithm

Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

let n = Int(String(readLine()!))!
let arr = readLine()!.split(separator: " ").map{ Int(String($0))! }
var dp = Array(repeating: -9999, count: 100001) // ์ตœ์†Œ๊ฐ’ 0์œผ๋กœ ํ•˜๋ฉด ์•ˆ๋ผ!!!!!
dp[0] = arr[0]

for i in 1..<n {
    dp[i] = max(arr[i], arr[i] + dp[i - 1])
}

print(dp.max()!)
๋ฐ˜์‘ํ˜•