Skip to content
This repository was archived by the owner on Apr 27, 2025. It is now read-only.

Commit 2b97e89

Browse files
authored
Create 62. Unique Paths.md
1 parent 06f3017 commit 2b97e89

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

62. Unique Paths.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 62. Unique Paths
2+
3+
### 2020-07-31
4+
5+
A robot is located at the top-left corner of a *m* x *n* grid (marked 'Start' in the diagram below).
6+
7+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
8+
9+
How many possible unique paths are there?
10+
11+
![img](https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png)
12+
13+
Above is a 7 x 3 grid. How many possible unique paths are there?
14+
15+
16+
17+
**Example 1:**
18+
19+
```
20+
Input: m = 3, n = 2
21+
Output: 3
22+
Explanation:
23+
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
24+
1. Right -> Right -> Down
25+
2. Right -> Down -> Right
26+
3. Down -> Right -> Right
27+
```
28+
29+
**Example 2:**
30+
31+
```
32+
Input: m = 7, n = 3
33+
Output: 28
34+
```
35+
36+
37+
38+
**Constraints:**
39+
40+
- `1 <= m, n <= 100`
41+
- It's guaranteed that the answer will be less than or equal to `2 * 10 ^ 9`.
42+
43+
44+
# Solution
45+
46+
```swift
47+
class Solution {
48+
func uniquePaths(_ m: Int, _ n: Int) -> Int {
49+
var cache = [[Int]].init(repeating: [Int].init(repeating: 0, count: n), count: m)
50+
for y in 1...m {
51+
for x in 1...n {
52+
if x == 1 || y == 1 {
53+
cache[y - 1][x - 1] = 1
54+
} else {
55+
cache[y - 1][x - 1] = cache[y - 2][x - 1] + cache[y - 1][x - 2]
56+
}
57+
}
58+
}
59+
return cache[m - 1][n - 1]
60+
}
61+
}
62+
63+
```
64+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy