原题链接在这里: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的更多相关文章

  1. 【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 ...

  2. LeetCode 1102. Path With Maximum Minimum Value

    原题链接在这里:https://leetcode.com/problems/path-with-maximum-minimum-value/ 题目: Given a matrix of integer ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. C#月份和日期转大写和C#集合分组

    //日转化为大写 private static string DaytoUpper(int day, string type) { if (day < 20) { return MonthtoU ...

  2. opentsdb操作草稿

    插入数据api/put 192.168.1.68:4242/api/put?details http://localhost:4242/api/put?detailsmethod:POST[ { &q ...

  3. mongoDB 分组并对分组结果筛选类似于SQL中的(group by xxx having ) 附带Java代码

    今天需要做一个筛选程序,因为数据放在mongodb中,没写过分组的查询语句,查了一些资料,终于写出来了,分享给各位小伙伴 需求是 查询 学员 在2019-07-29之后未同步的数据(同一个学员需要2条 ...

  4. win10 bcdedit testsigning

    win10 bcdedit testsigning # 禁用系统完整性检查和禁用驱动签名以及进入测试签名驱动模式> bcdedit.exe /set nointegritychecks on & ...

  5. 复杂dic的文件化存储和读取问题

    今天遇到一个难题.整出一个复杂的dic,里面不仅维度多,还含有numpy.array.超级复杂.过程中希望能够存储一下,万一服务器停了呢?万一断电了呢? 结果存好存,取出来可就不是那样了.网上搜索了很 ...

  6. Matlab外观模式

    外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口.本文以计算机为例,用Matlab代码实现外观模式.计算机包括CPU.内存以及硬盘等这些部件.用户 ...

  7. C#中的委托、事件及事件的订阅

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. v-for循环列表,展开样式随手风琴

    <div class="product_box" v-for="(item,index) of kinds" :key="index" ...

  9. SpringBoot 传入JSON对象参数

    1.请求参数格式必须是正确的JSON. 2.在入参中使用注解@RequestBody,用于接收JSON参数,使其自动转对象 3.关于lombok在此产生的一点小坑,@Builder对@RequestB ...

  10. 如何在SAP Cloud Platform上进行第一个integration flow开发

    登录SAP Cloud Platform integration tenant,点击Edit图标: 创建一个新的Content package: 保存content package后,点击artifa ...