LeetCode 1219. Path with Maximum Gold

原题链接在这里:https://leetcode.com/problems/path-with-maximum-gold/

题目:

In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.

Return the maximum amount of gold you can collect under the conditions:

  • Every time you are located in a cell you will collect all the gold in that cell.
  • From your position you can walk one step to the left, right, up or down.
  • You can‘t visit the same cell more than once.
  • Never visit a cell with 0 gold.
  • You can start and stop collecting gold from any position in the grid that has some gold.

Example 1:

Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
 [5,8,7],
 [0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.

Example 2:

Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
 [2,0,6],
 [3,4,5],
 [0,3,0],
 [9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.

Constraints:

  • 1 <= grid.length, grid[i].length <= 15
  • 0 <= grid[i][j] <= 100
  • There are at most 25 cells containing gold.

题解:

For each cell in the grid, start DFS.

DFS state is current index and current sum. And it should return starting at current index, the maximum glod it could get.

If current index is illegal, return current sum.

Otherwise, accumlate grid[i][j] to sum. And for each direction, do DFS. Get the maximum among 4 directions, and return.

Before return, backtracking grid[i][j] to its original value.

Time Complexity: expontential.

Space: O(m*n). m = grid.length. n = grid[0].length.

AC Java:

 1 class Solution {
 2     int [][] grid;
 3     int m;
 4     int n;
 5     int [][] dirs = new int[][]{{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
 6
 7     public int getMaximumGold(int[][] grid) {
 8         if(grid == null || grid.length == 0 || grid[0].length == 0){
 9             return 0;
10         }
11
12         int res = 0;
13         this.grid = grid;
14         this.m = grid.length;
15         this.n = grid[0].length;
16         for(int i = 0; i<m; i++){
17             for(int j = 0; j<n; j++){
18                 int sum = dfs(i, j, 0);
19                 res = Math.max(res, sum);
20             }
21         }
22
23         return res;
24     }
25
26     private int dfs(int i, int j, int sum){
27         if(i<0 || i>=m || j<0 || j>=n || grid[i][j]==0){
28             return sum;
29         }
30
31
32         sum += grid[i][j];
33         int temp = grid[i][j];
34         grid[i][j] = 0;
35
36         int max = 0;
37         for(int [] dir : dirs){
38             int x = i + dir[0];
39             int y = j + dir[1];
40
41             max = Math.max(max, dfs(x, y, sum));
42         }
43
44         grid[i][j] = temp;
45         return max;
46     }
47 }

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11791324.html

时间: 2024-08-04 18:23:21

LeetCode 1219. Path with Maximum Gold的相关文章

LeetCode OJ - Binary Tree Maximum Path

这道题需要注意的地方有以下一些: 1. 求从子树中的某节点到当前节点的最大路径不能采用递归方法,因为这个部分会被反复的调用,如果用递归,会使得之前已经计算过的节点被重复计算,使得时间复杂度特别高: 2. 树中有节点的值是负数的. 下面是AC代码.(我发现AC并不代表代码真的完全正确!!) 1 /** 2 * Given a binary tree, find the maximum path sum. 3 * The path may start and end at any node in t

[Leetcode][Tree][Binary Tree Maximum Path Sum]

找书中权值和最大的路径,至少包含一个点. 有点类似LCA(最近公共祖先),树的问题关键是如何划分子问题,然后递归求解. 想到了可以返回两种结果,一个是单独路径的最大和,一种是子树的最大和,然后在求解的过程中不断的更新答案. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val

【Leetcode】Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路:与[Leetcode]Path Sum 不同

LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方案

原始题目如下,意为寻找数组和最大的子串,返回这个最大和即可. Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6.

LeetCode:Path Sum - 树的根节点到叶节点的数字之和

1.题目名称 Path Sum(树的根节点到叶节点的数字之和) 2.题目地址 https://leetcode.com/problems/path-sum/ 3.题目内容 英文:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. 中文:给定一颗二叉树,如

LeetCode &quot;Minimum Path Sum&quot; - 2D DP

An intuitive 2D DP: dp[i][j] = min(grid[i-1][j-1] + dp[i-1][j], grid[i-1][j-1] + dp[i][j+1]) class Solution { public: int minPathSum(vector<vector<int> > &grid) { // dp[i][j] = min(dp[i-1][j] + dp[i][j], dp[i][j-1] + dp[i][j]); int n = gri

【LeetCode】Path Sum

题目 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true,

Leetcode:Minimum Path Sum 矩形网格最小路径和

Minimum Path Sum: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 解题分析: 每次只能向下或者向

[leetcode]Simplify Path @ Python

原题地址:https://oj.leetcode.com/problems/simplify-path/ 题意: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" click to show corn