-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique_paths.rs
93 lines (75 loc) · 2.3 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::collections::HashMap;
#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
struct Position {
row: usize,
col: usize
}
impl Position {
fn new(row: usize, col: usize) -> Self {
Self { row, col }
}
fn go_down(&self) -> Self {
Self::new(self.row + 1, self.col)
}
fn go_right(&self) -> Self {
Self::new(self.row, self.col + 1)
}
fn is_valid(&self, m: usize, n: usize) -> bool {
self.row < m && self.col < n
}
}
/// There is a robot on an `m x n` grid. The robot is initially located at the top-left corner
/// (i.e., `grid[0][0]`). The robot tries to move to the bottom-right corner (i.e.
/// `grid[m-1][n-1]`). The robot can only move either down or right at any point in time.
///
/// Given the two integers `m` and `n`, return the number of possible unique paths that the robot
/// can take to reach the bottom-right corner.
///
/// The test cases are generated so that the answer will be less than or equal to `2 * 10^9`.
struct Solution;
impl Solution {
fn worker(results: &mut HashMap<Position, i32>, m: usize, n: usize, pos: Position) -> i32 {
if results.contains_key(&pos) {
results[&pos]
} else {
let mut result = 0;
let down = pos.go_down();
if down.is_valid(m, n) {
result += Self::worker(results, m, n, down);
}
let right = pos.go_right();
if right.is_valid(m, n) {
result += Self::worker(results, m, n, right);
}
results.insert(pos, result);
result
}
}
pub fn unique_paths(m: i32, n: i32) -> i32 {
let m = m as usize;
let n = n as usize;
let mut results = HashMap::new();
let end = Position::new(m - 1, n - 1);
results.insert(end, 1);
let start = Position::new(0, 0);
Self::worker(&mut results, m, n, start)
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let m = 3;
let n = 7;
let result = Solution::unique_paths(m, n);
assert_eq!(result, 28);
}
#[test]
fn example_2() {
let m = 3;
let n = 2;
let result = Solution::unique_paths(m, n);
assert_eq!(result, 3);
}
}