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
0gold. - 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 <= 150 <= 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 it should return starting at current index, the maximum glod it could get.
If current index is illegal, return 0.
Otherwise, return grid[i][j] + 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: O(m^2*n^2).
Space: O(m*n). m = grid.length. n = grid[0].length.
AC Java:
class Solution {
int res = 0;
int [][] dirs = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public int getMaximumGold(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(grid[i][j] != 0){
res = Math.max(res, dfs(grid, i, j, m, n));
}
}
}
return res;
}
private int dfs(int [][] grid, int i, int j, int m, int n){
if(i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0){
return 0;
}
int gold = grid[i][j];
grid[i][j] = 0;
int max = 0;
for(int [] dir : dirs){
max = Math.max(max, dfs(grid, i + dir[0], j + dir[1], m, n));
}
grid[i][j] = gold;
return gold + max;
}
}
LeetCode 1219. Path with Maximum Gold的更多相关文章
- 【leetcode】1219. Path with Maximum Gold
题目如下: In a gold mine grid of size m * n, each cell in this mine has an integer representing the amou ...
- LeetCode 1102. Path With Maximum Minimum Value
原题链接在这里:https://leetcode.com/problems/path-with-maximum-minimum-value/ 题目: Given a matrix of integer ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 437. Path Sum III 路径和 III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- leetcode@ [124] Binary Tree Maximum Path Sum (DFS)
https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...
- [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
随机推荐
- JS中,JSON 和 对象互转,数组和字符串的转换?
JSON 与 J对象转化 要实现从对象转换为 JSON 字符串,使用 JSON.stringify() 方法: 如下: var json = JSON.stringify({a: 'Hello', b ...
- 关于nslookup以及dig命令的研究报告
我们在日常上网时都是用域名访问网路,如www.baidu.com,而在实际寻址过程中,是使用IP地址,如180.101.49.11,域名到IP地址的解析是通过DNS服务器来实现的,系统中我们可以用一些 ...
- KepServerEX读写三菱PLC,车间现场测试记录,带你了解【数据采集的困境】的前世与今生
1.不了解KepServerEX 的鞋童,可以先了解一下OPC UA,OPC UA服务端.我们当前项目读写三菱PLC是自己写的类库,但我感觉调用不够方便灵活,工作之余用OPC UA方式尝试一下 2.数 ...
- ubuntu安装shadow socks-qt5
Ubuntu16安装shadow socks-qt5 在Ubuntu下也是有GUI客户端,怎么安装请看下面: 首先,针对Ubuntu16的版本可以直接这么安装: .$ sudo add-apt-rep ...
- HTTP响应状态
状态码分类 状态码详解 状态码 英文提示 说明 100 Continue 继续 101 Switching Protocols 切换协议.服务器根据客户端的请求切换协议.只能切换到更高级的协议,例如, ...
- ubuntu安装texlive2019
1.下载texlive2019的iso文件,清华镜像地址:https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/Images/texliv ...
- C# 8.0 中开启默认接口实现
原文:C# 8.0 中开启默认接口实现 当你升级到 C# 8.0 和 .NET Core 3.0 之后,你就可以开始使用默认接口实现的功能了. 从现在开始,你可以在接口里面添加一些默认实现的成员,避免 ...
- C# 对象集合初始化
一.自动实现的属性 public class Person { // C# 3之前我们定义属性时,一般会像下面这样去定义 // 首先会先定义私有字段,再定义属性来对字段进行访问 //private s ...
- Go内存分配器可视化指南【译】【精】
当我第一次开始尝试理解 Go 语言的内存分配器时,整个过程让我抓狂.一切看起来都像一个神秘的黑盒子.因为几乎所有技术魔法(technical wizardry)都隐藏在抽象之下,所以你需要一层一层的剥 ...
- 设置断点调式 fiddler
1. 用IE 打开博客园的登录界面 http://passport.cnblogs.com/login.aspx 2. 打开Fiddler, 在命令行中输入bpu http://passport. ...