2020-02-03 17:46:04

问题描述

问题求解

非常好的题目,和two thumb其实非常类似,但是还是有个一点区别,就是本题要求最后要到达(n - 1, n - 1),只有到达了(n - 1, n - 1)才算是有效解,two thumb是一定会有解的,所以不用加特别判断。

也是一种路径规划类的题目,难点依然是状态的表示,我们这里使用的p1,p2的坐标位置作为状态。

另外,还需要注意的是在超界的时候,我们需要返回的是Integer.MIN_VALUE,这样就可以规避掉一些中间节点到不了终点的情况。

    int[][][] dp = new int[51][51][51];

    public int cherryPickup(int[][] grid) {
int n = grid.length;
for (int i = 0; i <= 50; i++) {
for (int j = 0; j <= 50; j++) {
Arrays.fill(dp[i][j], -1);
}
}
int res = dfs(grid, 0, 0, 0);
return dp[n - 1][n - 1][n - 1] == -1 ? 0 : res;
} private int dfs(int[][] grid, int x1, int y1, int x2) {
int n = grid.length;
int y2 = x1 + y1 - x2;
if (x1 >= n || y1 >= n || x2 >= n || y2 >= n) return Integer.MIN_VALUE;
if (dp[x1][y1][x2] != -1) return dp[x1][y1][x2];
else if (x1 == n - 1 && y1 == n - 1) dp[x1][y1][x2] = grid[n - 1][n - 1];
else if (grid[x1][y1] == -1 || grid[x2][y2] == -1) dp[x1][y1][x2] = Integer.MIN_VALUE;
else {
int curr = x1 == x2 && y1 == y2 ? grid[x1][y1] : grid[x1][y1] + grid[x2][y2];
dp[x1][y1][x2] = curr + Math.max(Math.max(dfs(grid, x1 + 1, y1, x2 + 1), dfs(grid, x1 + 1, y1, x2)), Math.max(dfs(grid, x1, y1 + 1, x2 + 1), dfs(grid, x1, y1 + 1, x2)));
}
return dp[x1][y1][x2];
}

  

动态规划-Cherry Pickup的更多相关文章

  1. LeetCode741. Cherry Pickup

    https://leetcode.com/problems/cherry-pickup/description/ In a N x N grid representing a field of che ...

  2. [LeetCode] 741. Cherry Pickup 捡樱桃

    In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...

  3. [LeetCode] Cherry Pickup 捡樱桃

    In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...

  4. [Swift]LeetCode741. 摘樱桃 | Cherry Pickup

    In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...

  5. 741. Cherry Pickup

    In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...

  6. LeetCode 741. Cherry Pickup

    原题链接在这里:https://leetcode.com/problems/cherry-pickup/ 题目: In a N x N grid representing a field of che ...

  7. 动态规划Dynamic Programming

    动态规划Dynamic Programming code教你做人:DP其实不算是一种算法,而是一种思想/思路,分阶段决策的思路 理解动态规划: 递归与动态规划的联系与区别 -> 记忆化搜索 -& ...

  8. leetcode动态规划题目总结

    Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...

  9. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

随机推荐

  1. redis命令学习(二) · THIS SPACE

    列表(Lists)操作命令 Redis列表是简单的字符串列表,按照插入顺序排序. 你可以添加一个元素导列表的头部(左边)或者尾部(右边)LPUSH命令插入一个新的元素导头部,而RPUSH插入一个新元素 ...

  2. PHP创建缓存文件

    文件操作类 <?php /** 文件操作类 */ class FileIO { /** * 读取目录 * @param string $dirPath dir名称 * @return array ...

  3. 嗨! Apriori算法

    Association Rule 一:项集和规则 1.1 认识名词: Association Rule : 关联规则 Frequent Itemsets : 频繁项集 Sequential Patte ...

  4. CSS实现响应式布局

    用CSS实现响应式布局 响应式布局感觉很高大上,很难,但实际上只用CSS也能实现响应式布局要用的就是CSS中的没接查询,下面就介绍一下怎么运用: 使用@media 的三种方法 1.直接在CSS文件中使 ...

  5. 【译文连载】 理解Istio服务网格(第六章 可观测性)

    全书目录 第一章 概述 第二章 安装 第三章 流控 第四章 服务弹性 第五章 混沌测试 ​本文目录 第6章 可观测性 6.1 分布式调用链跟踪(tracing) 6.1.1 基本概念 6.1.2 Ja ...

  6. vmware企业虚拟化平台vSphere管理与配置

    ├─1-CCIE-DC课程介绍.avi ├─2-vSphere-简介.avi ├─3-vSphere-新功能介绍.avi ├─4-vSphere-授权介绍.avi ├─5-vSphere-课程拓扑介绍 ...

  7. MacOS麦克风输入监听的方法

    Windows上很多人都知道,然而实际上并没什么用——延迟太大,根本没法用. MacOS上有两种方法: QuickTime Player新建音频录制(不需要真的录音),如下图: 这个方法和Window ...

  8. HTML5 基础知识(1)——基本标签

    ## HTML**概念**:是最基础的网页开发语言(Hyper Text Markup Langage 超文本标记语言) > 1.超文本:超文本是用超链接的方式i,将各种不同空间的文字组织在一起 ...

  9. ZXingObjC二维码扫描

    #import "QRScanViewController.h" #import "AppDelegate.h" @interface QRScanViewCo ...

  10. python的基本数据类型简介

    python的基本数据类型有:数字-numbers.字符串-str.列表-list.元组-tuple.字典-dict.布尔-bool.集合-set 下面来个概览先大概了解一下,后面博文中咱再细说- 1 ...