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 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 ...
随机推荐
- go编译运行说明
二 编译运行说明 1.1 编译 1)有了go源文件,通过编译器将其编译成机器可以识别的二进制码文件. 2)在该源文件目录下,通过 go build 对hello.go 文件进行编译.可以指定生成的可 ...
- Go语言【开发】加载JSON配置文件
JSON配置加载 辅助网址,JSON转结构体对应 http://json2struct.mervine.net/ 从JSON文件中加载配置到全局变量中 配置文件 config.json { &quo ...
- golang 之 sql
golang提供了sql包查询数据 建立连接 导入第三方包 import( "database/sql" _"github.com/go-sql-driver/mysql ...
- H5页面跳转与传值
页面之间的跳转经常使用a标签,使用mvc框架的都是通过访问controller的请求方法,返回请求页面.但本次开发,前端与后台完全分离,前端APP使用HBuider来开发,后台数据就无法使用mvc框架 ...
- java之spring之依赖注入
一.DI: Dependency injection; 依赖注入 依赖注入和控制反转是同一个概念的不同说法. 对象的创建依赖于容器.对象属性的设置是由容器来设置. 对象属性的赋值过程称为注入. 二.S ...
- C# vb .net实现相机视图效果滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的相机视图效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...
- Django:内置组件Content-Type
12.Django组件之Content_Type 1.帮助我们生成了一张表,里面有所有表名.这样不再自建表在表中填表名,用Foreignkey获取 2.为了让我们快速进入插入数据,填写一个字段Gene ...
- 使用 Create-React-App 开发 Chrome 扩展
整理 Kindle 标注.书签和笔记从未如此简单! Kindle 标注管理应用 Kindle Mate 只支持 Windows,不支持 Mac.标注只是解析我的剪贴文本文件,配合 FileReader ...
- Spring 在xml文件中配置Bean
Spring容器是一个大工厂,负责创建.管理所有的Bean. Spring容器支持2种格式的配置文件:xml文件.properties文件,最常用的是xml文件. Bean在xml文件中的配置 < ...
- 如何在SAP gateway系统配置路由到后台系统的OData服务路径
看这张架构图,SAP Gateway系统也叫frontend系统,通过RFC远程调用SAP后台系统的OData服务实现. 以SAP CRM Fiori应用My Opportunity为例,使用事务码/ ...