Created
August 22, 2021 10:37
-
-
Save masakih/ffc70a6a9ccbf4331b222ce5ffa2baa0 to your computer and use it in GitHub Desktop.
algo306
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let aa = [3, 1, 4, 1, 5, 9, 2, 6] | |
| func getInt2() -> (Int, Int) { (aa.count, 4) } | |
| func getIntArray() -> [Int] { aa } | |
| /// | |
| let (n, m) = getInt2() | |
| let a = getIntArray() | |
| var dp = Array<Int?>(repeating: nil, count: n) | |
| dp[0] = 0 | |
| func resolve(_ n: Int) -> Int { | |
| if let k = dp[n] { | |
| return k | |
| } | |
| let d = n > m ? m : n | |
| let k = (1...d) | |
| .reduce(Int.max) { min($0, resolve(n - $1) + $1 * a[n]) } | |
| dp[n] = k | |
| return k | |
| } | |
| print( | |
| resolve(n - 1) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment