forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1219
No.1219.Path with Maximum Gold
- Loading branch information
Showing
9 changed files
with
495 additions
and
405 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
12 changes: 5 additions & 7 deletions
12
solution/1200-1299/1219.Path with Maximum Gold/Solution.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,22 @@ | ||
class Solution { | ||
public: | ||
vector<vector<int>> grid; | ||
vector<int> dirs = {-1, 0, 1, 0, -1}; | ||
|
||
int getMaximumGold(vector<vector<int>>& grid) { | ||
this->grid = grid; | ||
int ans = 0; | ||
for (int i = 0; i < grid.size(); ++i) | ||
for (int j = 0; j < grid[0].size(); ++j) | ||
ans = max(ans, dfs(i, j)); | ||
ans = max(ans, dfs(i, j, grid)); | ||
return ans; | ||
} | ||
|
||
int dfs(int i, int j) { | ||
int dfs(int i, int j, vector<vector<int>>& grid) { | ||
if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size() || grid[i][j] == 0) return 0; | ||
int t = grid[i][j]; | ||
grid[i][j] = 0; | ||
int res = 0; | ||
for (int k = 0; k < 4; ++k) res = max(res, t + dfs(i + dirs[k], j + dirs[k + 1])); | ||
int ans = 0; | ||
for (int k = 0; k < 4; ++k) ans = max(ans, t + dfs(i + dirs[k], j + dirs[k + 1], grid)); | ||
grid[i][j] = t; | ||
return res; | ||
return ans; | ||
} | ||
}; |
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
Oops, something went wrong.