Skip to content

Commit 23f16f3

Browse files
committed
LC 1219. Path with Maximum Gold (Rust BT/DFS)
1 parent 86f2a45 commit 23f16f3

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
453453
| [1171. Remove Zero Sum Consecutive Nodes from Linked List][lc1171] | 🟠 Medium | [![python](res/py.png)][lc1171py] |
454454
| [1187. Make Array Strictly Increasing][lc1187] | 🔴 Hard | [![python](res/py.png)][lc1187py] |
455455
| [1207. Unique Number of Occurrences][lc1207] | 🟢 Easy | [![python](res/py.png)][lc1207py] [![rust](res/rs.png)][lc1207rs] |
456+
| [1219. Path with Maximum Gold][lec1219] | 🟠 Medium | [![rust](res/rs.png)][lc1219rs] |
456457
| [1220. Count Vowels Permutation][lc1220] | 🔴 Hard | [![python](res/py.png)][lc1220py] [![rust](res/rs.png)][lc1220rs] |
457458
| [1232. Check If It Is a Straight Line][lc1232] | 🟢 Easy | [![rust](res/rs.png)][lc1232rs] |
458459
| [1235. Maximum Profit in Job Scheduling][lc1235] | 🔴 Hard | [![python](res/py.png)][lc1235py] [![rust](res/rs.png)][lc1235rs] |
@@ -1710,6 +1711,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
17101711
[lc1207]: https://leetcode.com/problems/unique-number-of-occurrences/
17111712
[lc1207py]: leetcode/unique-number-of-occurrences.py
17121713
[lc1207rs]: leetcode/unique-number-of-occurrences.rs
1714+
[lc1219]: https://leetcode.com/problems/path-with-maximum-gold/
1715+
[lc1219rs]: leetcode/path-with-maximum-gold.rs
17131716
[lc1220]: https://leetcode.com/problems/count-vowels-permutation/
17141717
[lc1220py]: leetcode/count-vowels-permutation.py
17151718
[lc1220rs]: leetcode/count-vowels-permutation.rs

leetcode/path-with-maximum-gold.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// 1219. Path with Maximum Gold
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/path-with-maximum-gold/
5+
//
6+
// Tags: Array - Backtracking - Matrix
7+
8+
struct Solution;
9+
impl Solution {
10+
/// We can use DFS, from each cell, do a dfs while marking the current cell as "visited", when
11+
/// we visit a cell that we are not allowed to visit, or go out of bounds, return 0, for each
12+
/// branch, return the maximum between its children.
13+
///
14+
/// Time complexity: O(m*n*4^(m*n)) - We visit each cell and, for each, launch a dfs. Each dfs
15+
/// branches in a maximum of 4 calls at each level and it can have as many levels as the number
16+
/// of cells that contain gold.
17+
/// Space complexity: O(m*n) - The height of the call stack, which can grow to the number of
18+
/// cells that contain gold and it maxes out at the size of the input matrix.
19+
///
20+
/// Runtime 61 ms Beats 50%
21+
/// Memory 2.04 MB Beats 100%
22+
pub fn get_maximum_gold(mut grid: Vec<Vec<i32>>) -> i32 {
23+
fn dfs(grid: &mut Vec<Vec<i32>>, row: i32, col: i32) -> i32 {
24+
if row < 0
25+
|| row >= grid.len() as i32
26+
|| col < 0
27+
|| col >= grid[0].len() as i32
28+
|| grid[row as usize][col as usize] == 0
29+
{
30+
return 0;
31+
}
32+
let mut gold = 0;
33+
let current_gold = grid[row as usize][col as usize];
34+
grid[row as usize][col as usize] = 0;
35+
for (i, j) in [(-1, 0), (1, 0), (0, -1), (0, 1)] {
36+
gold = gold.max(dfs(grid, row + i, col + j));
37+
}
38+
grid[row as usize][col as usize] = current_gold;
39+
gold + current_gold
40+
}
41+
42+
let mut gold = 0;
43+
for row in 0..grid.len() {
44+
for col in 0..grid[0].len() {
45+
gold = gold.max(dfs(&mut grid, row as i32, col as i32));
46+
}
47+
}
48+
gold
49+
}
50+
}
51+
52+
// Tests.
53+
fn main() {
54+
let tests = [
55+
(vec![vec![0, 6, 0], vec![5, 8, 7], vec![0, 9, 0]], 24),
56+
(
57+
vec![
58+
vec![1, 0, 7],
59+
vec![2, 0, 6],
60+
vec![3, 4, 5],
61+
vec![0, 3, 0],
62+
vec![9, 0, 20],
63+
],
64+
28,
65+
),
66+
];
67+
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
68+
let mut success = 0;
69+
for (i, t) in tests.iter().enumerate() {
70+
let res = Solution::get_maximum_gold(t.0.clone());
71+
if res == t.1 {
72+
success += 1;
73+
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
74+
} else {
75+
println!(
76+
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
77+
i, t.1, res
78+
);
79+
}
80+
}
81+
println!();
82+
if success == tests.len() {
83+
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
84+
} else if success == 0 {
85+
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
86+
} else {
87+
println!(
88+
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
89+
tests.len() - success
90+
)
91+
}
92+
}

0 commit comments

Comments
 (0)