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 ...
随机推荐
- 手撕面试官系列(三):微服务架构Dubbo+Spring Boot+Spring Cloud
文章首发于今日头条:https://www.toutiao.com/i6712696637623370248/ 直接进入主题 Dubbo (答案领取方式见侧边栏) Dubbo 中 中 zookeepe ...
- 对JavaEE的简单理解
本文是在学习软件工程与J2EE课程时的学习笔记,旨在从大体的概念上了解Java EE的一些主要组件在Web应用中的作用. 上图精炼的描述了MVC模型以及Java EE的部分组件如何分布在一个Web应用 ...
- qt5信息提示框QMessageBox用法(很全)
information QMessageBox::information(NULL, "Title", "Content", QMessageBox::Yes ...
- sso cas 坑
一个中文文档地址: http://www.cassso-china.cn/apereo_github_cas_5.2/apereo.github.io/cas/5.2.x/ ============= ...
- redis 中文显示的问题解决方法
在redis 中存储中文,读取会出现乱码(其实不是乱码,只是不是我们存的中文显示) 1 redis> set test "我们" 2 OK 3 redis> get t ...
- C# 多线程与高并发处理并且具备暂停、继续、停止功能
--近期有一个需要运用多线程的项目,会有并发概率,所以写了一份代码,可能有写地方还不完善,后续有需求在改 1 /// <summary> /// 并发对象 /// </summary ...
- C# Socket keeplive 心跳检测实例
版权声明:本文为CSDN博主「b哈利路亚d」的原创文章,重新编辑发布,请尊重原作者的劳动成果,转载的时候附上原文链接:https://blog.csdn.net/lanwilliam/article/ ...
- Maven父子工程,子项目变灰,提示该项目已被移除出maven父工程
最近使用maven的父子工程结构搭建微服务架构时,不知道什么原因, 子工程总是被莫名移除出父工程,然后打包处的项目名变成了灰色, 重启该项目时会提示,“该子项目已被移除,是否删除该项目”,这个 当然不 ...
- php 弹窗案例
<?php // 弹出对话框并且返回原来的页面 echo "<script language=\"JavaScript\">\r\n"; ec ...
- MySQL AutoIncrement--自增锁模式
自增锁模式 在MYSQL 5.1.22版本前,自增列使用AUTO_INC Locking方式来实现,即采用一种特殊的表锁机制来保证并发插入下自增操作依然是串行操作,为提高插入效率,该锁会在插入语句完成 ...