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 ...
随机推荐
- BJFU-216-基于链式存储结构的图书信息表的修改
#include<stdio.h> #include<stdlib.h> #define MAX 100 typedef struct Book{ double no; cha ...
- Python之路【第二十二篇】:轮播图片CSS
轮播代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- golang的time包
Time对象转换为string和时间戳调用Time对象的方法 转换为string:Time.Format(输出的格式) 转换为时间戳 :Time.Unix() 两者转换为Time对象的时候调用的是ti ...
- 配置linux命令行界面的 文件显示颜色
在linux命令行界面下使用ls命令时,有时会看见显示的文件会有不同的颜色,因为linux的文件没有后缀名这个概念(Windows系统中的文件会有后缀名,从而可以将文件标识为不同类型),显示不同的颜色 ...
- 华为云实战开发】5.如何快速创建免费Git代码仓库【华为云技术分享】
1 文章目的 本文主要帮助已经掌握或者想要掌握Git的开发者,如何更好的应用Git,以及更好的将Git与DevCloud结合应用. 2 概述 2.1 版本控制系统介绍 从狭义上来说,版本控制系统是软件 ...
- C# vb .net图像合成-合成星形
在.net中,如何简单快捷地实现图像合成呢,比如合成文字,合成艺术字,多张图片叠加合成等等?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码 ...
- python 基础(文件)
文件句柄:可简单理解为应该内存对象 open()函数 参考 https://www.runoob.com/python3/python3-file-methods.html 读.写.追加 ''' t ...
- 自学Python编程的第五天(希望有IT大牛帮我看最下面的代码)----------来自苦逼的转行人
2019-09-15-15:40:24 今天没有学知识,是一个一周总结,把这一周学的知识总结一遍,然后把做过的练习题再做一遍 看是否还会有再出现同样的错误,而且还可以知道有哪些知识点没有掌握好,可以把 ...
- python二维数组切片
python中list切片的使用非常简洁.但是list不支持二维数组.仔细研究了一下发现,因为list不是像nampy数组那么规范.list非常灵活.所以没办法进行切片操作. 后来想了两个办法来解决: ...
- C#中的委托、事件及事件的订阅
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...