Created
May 14, 2024 15:57
-
-
Save lbvf50mobile/e4d26b619ea70ba8bea42437450696fc to your computer and use it in GitHub Desktop.
Leetcode: 1219. Path with Maximum Gold.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Leetcode: 1219. Path with Maximum Gold. | |
# https://leetcode.com/problems/path-with-maximum-gold/ | |
# = = = = = = = = = = = = = = | |
# Accepted. | |
# Thanks God, Jesus Christ! | |
# = = = = = = = = = = = = = = | |
# Runtime: 3375 ms, faster than 33.39% of Python3 online submissions for Path | |
# with Maximum Gold. | |
# Memory Usage: 16.4 MB, less than 94.92% of Python3 online submissions for | |
# Path with Maximum Gold. | |
# 2024.05.14. Daily Challenge. | |
class Solution: | |
def getMaximumGold(self, grid: List[List[int]]) -> int: | |
DIRECTIONS = [0, 1, 0, -1, 0] | |
rows = len(grid) | |
cols = len(grid[0]) | |
max_gold = 0 | |
def dfs_backtrack(grid, rows, cols, row, col): | |
# Base case: this cell is not in the matrix or has no gold | |
if row < 0 or col < 0 or row == rows or col == cols or \ | |
grid[row][col] == 0: | |
return 0 | |
max_gold = 0 | |
# Mark the cell as visited and save the value | |
original_val = grid[row][col] | |
grid[row][col] = 0 | |
# Backtrack in each of the four directions | |
for direction in range(4): | |
max_gold = max(max_gold, | |
dfs_backtrack(grid, rows, cols, | |
DIRECTIONS[direction] + row, | |
DIRECTIONS[direction + 1] + col)) | |
# Set the cell back to its original value | |
grid[row][col] = original_val | |
return max_gold + original_val | |
# Search for the path with the maximum gold starting from each cell | |
for row in range(rows): | |
for col in range(cols): | |
max_gold = max(max_gold, dfs_backtrack(grid, rows, cols, row, | |
col)) | |
return max_gold |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment