|
| 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