-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique_paths.rs
38 lines (38 loc) · 966 Bytes
/
unique_paths.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
///
/// Problem: Unique Paths
///
/// A robot is located at the **top-left corner** of an `m x n` grid.
/// The robot can only **move right or down** at any point in time.
/// The robot is trying to reach the **bottom-right corner** of the grid.
///
/// How many possible unique paths are there?
///
/// **Example 1:**
/// ```plaintext
/// Input: m = 3, n = 7
/// Output: 28
/// ```
///
/// **Example 2:**
/// ```plaintext
/// Input: m = 3, n = 2
/// Output: 3
/// Explanation: The possible paths are: "Right → Down → Down", "Down → Down → Right", "Down → Right → Down".
/// ```
///
/// **Constraints:**
/// - `1 <= m, n <= 100`
///
/// # Solution:
/// - **Time Complexity:** `O(1)`
/// - **Space Complexity:** `O(1)`
impl Solution {
pub fn unique_paths(m: i32, n: i32) -> i32 {
let (m, n) = (m as u64, n as u64);
let mut res = 1;
for i in 1..n {
res = res * (m + i - 1) / i;
}
res as i32
}
}