/다리 놓기(BottomUp).swift Secret
Created
April 2, 2023 15:29
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
var cache = [[Int]](repeating: [Int](repeating: 0, count: 31), count: 31) | |
for i in 0...30 { | |
for j in 0...i { | |
if i == j || j == 0 { | |
cache[i][j] = 1 | |
continue | |
} | |
cache[i][j] = cache[i - 1][j - 1] + cache[i - 1][j] | |
} | |
} | |
func solution() { | |
let input = readLine()!.split(separator: " ").map { Int($0)! } | |
let r = input[0], n = input[1] | |
print(cache[n][r]) | |
} | |
let t = Int(readLine()!)! | |
for _ in 0..<t { solution() } |
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
var cache = [[Int]](repeating: [Int](repeating: 0, count: 31), count: 31) | |
func f(n: Int, r: Int) -> Int { | |
if cache[n][r] != 0 { | |
return cache[n][r] | |
} | |
if n == r || r == 0 { | |
return 1 | |
} | |
cache[n][r] = f(n: n - 1, r: r) + f(n: n - 1, r: r - 1) | |
return cache[n][r] | |
} | |
func solution() { | |
let input = readLine()!.split(separator: " ").map { Int($0)! } | |
let r = input[0], n = input[1] | |
print(f(n: n, r: r)) | |
} | |
let t = Int(readLine()!)! | |
for _ in 0..<t { solution() } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment