반응형
문제
https://leetcode.com/problems/unique-paths/
———————————————————————————
코드
class Solution {
func uniquePaths(_ m: Int, _ n: Int) -> Int {
var map: [[Int]] = Array(repeating: Array(repeating: 0, count: n), count: m)
for x in 0..<n {
map[0][x] = 1
}
for y in 1..<m {
for x in 0..<n {
var count = 0
let up = y - 1
count += map[up][x]
let left = x - 1
if left >= 0 {
count += map[y][left]
}
map[y][x] = count
}
}
return map[m-1][n-1]
}
}
———————————————————————————
설명
경로는 down과 right만 가능하니,
윗칸의 경로수와 오른쪽 칸의 경로수를 더하여 쭉쭉 나가면 됩니다.
———————————————————————————
회고
너무 쉬운거 같은데 제가 잘못 접근한건가요?;;;;;
반응형
'코딩 테스트 > LeetCode(swift)' 카테고리의 다른 글
[LeetCode] 133. Clone Graph (0) | 2022.02.20 |
---|---|
[LeetCode] 55. Jump Game (0) | 2022.02.20 |
[LeetCode] 91. Decode Ways (0) | 2022.02.19 |
[LeetCode] 213. House Robber II (0) | 2022.02.19 |
[LeetCode] 198. House Robber (0) | 2022.02.19 |