LeetCode 741. Cherry Pickup
原题链接在这里:https://leetcode.com/problems/cherry-pickup/
题目:
In a N x N grid representing a field of cherries, each cell is one of three possible integers.
- 0 means the cell is empty, so you can pass through;
- 1 means the cell contains a cherry, that you can pick up and pass through;
- -1 means the cell contains a thorn that blocks your way.
Your task is to collect maximum number of cherries possible by following the rules below:
- Starting at the position (0, 0) and reaching (N-1, N-1) by moving right or down through valid path cells (cells with value 0 or 1);
- After reaching (N-1, N-1), returning to (0, 0) by moving left or up through valid path cells;
- When passing through a path cell containing a cherry, you pick it up and the cell becomes an empty cell (0);
- If there is no valid path between (0, 0) and (N-1, N-1), then no cherries can be collected.
Example 1:
Input: grid =
[[0, 1, -1],
[1, 0, -1],
[1, 1, 1]]
Output: 5
Explanation:
The player started at (0, 0) and went down, down, right right to reach (2, 2).
4 cherries were picked up during this single trip, and the matrix becomes [[0,1,-1],[0,0,-1],[0,0,0]].
Then, the player went left, up, up, left to return home, picking up one more cherry.
The total number of cherries picked up is 5, and this is the maximum possible.
Note:
gridis anNbyN2D array, with1 <= N <= 50.- Each
grid[i][j]is an integer in the set{-1, 0, 1}. - It is guaranteed that grid[0][0] and grid[N-1][N-1] are not -1.
题解:
It could be understanding as two people collecting cheeries from (n-1, n-1) to (0, 0).
Two people cooridinates are (x1, y1), (x2, y2).
dfs(x1, y1, x2, y2) returns maximum cheeries collected from two cooridinates to (0, 0).
Thus max(x1, y1, x2, y2) = grid[x1][y1] + grid[x2][y2] + max(dfs(x1-1, y1, x2-1, y2), dfs(x1, y1-1, x2, y2-1), dfs(x1-1, y1, x2, y2-1), dfs(x1, y1-1, x2-1, y2)).
First person could move from top, or left. Second person could do the same. Totally 4 combinations.
And of cource, if x1==y1, which means both people are on the same grid, its cheery can't be collected twice.
y2 = x1+y1-x2. since both of them have same total steps.
Time Complexity: O(n^3).
Space: O(n^3).
AC Java:
class Solution {
int [][][] dp;
int n;
public int cherryPickup(int[][] grid) {
if(grid == null || grid.length == 0 || grid[0].length == 0){
return 0;
}
n = grid.length;
dp = new int[n][n][n];
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
Arrays.fill(dp[i][j], Integer.MIN_VALUE);
}
}
return Math.max(0, dfs(grid, n-1, n-1, n-1));
}
private int dfs(int [][] grid, int x1, int y1, int x2){
int y2 = x1+y1-x2;
if(x1<0 || y1<0 || x2<0 || y2<0){
return -1;
}
if(grid[x1][y1]<0 || grid[x2][y2]<0){
return -1;
}
if(dp[x1][y1][x2] != Integer.MIN_VALUE){
return dp[x1][y1][x2];
}
if(x1==0 && y1==0){
dp[0][0][0] = grid[0][0];
return grid[0][0];
}
int res = Math.max(Math.max(dfs(grid, x1-1, y1, x2-1), dfs(grid, x1, y1-1, x2)), Math.max(dfs(grid, x1-1, y1, x2), dfs(grid, x1, y1-1, x2-1)));
if(res < 0){
dp[x1][y1][x2] = -1;
return -1;
}
res += grid[x1][y1];
if(x1 != x2){
res += grid[x2][y2];
}
dp[x1][y1][x2] = res;
return res;
}
}
LeetCode 741. Cherry Pickup的更多相关文章
- [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 ...
- 741. Cherry Pickup
In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...
- [LeetCode] Cherry Pickup 捡樱桃
In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...
- [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 ...
- LeetCode741. Cherry Pickup
https://leetcode.com/problems/cherry-pickup/description/ In a N x N grid representing a field of che ...
- Java实现 LeetCode 741 摘樱桃(DFS || 递推 || 传纸条)
741. 摘樱桃 一个N x N的网格(grid) 代表了一块樱桃地,每个格子由以下三种数字的一种来表示: 0 表示这个格子是空的,所以你可以穿过它. 1 表示这个格子里装着一个樱桃,你可以摘到樱桃然 ...
- 动态规划-Cherry Pickup
2020-02-03 17:46:04 问题描述: 问题求解: 非常好的题目,和two thumb其实非常类似,但是还是有个一点区别,就是本题要求最后要到达(n - 1, n - 1),只有到达了(n ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
随机推荐
- Go基础编程实践(四)—— 数组和map
数组去重 package main import "fmt" func main(){ intSlice := []int{1,5,5,5,5,7,8,6,6, 6} fmt.Pr ...
- python_进程与线程的补充
进程与线程的标识 知识点一:进程id 与 线程ident import time import multiprocessing import threading time.sleep(10) prin ...
- python 跨目录访问文件
1.同级.同目录的文件之间的访问 有这样一个目录结构 假如,in_A.py 这个文件想调用 hello_world.py 中的函数怎么办呢? --->>> import 只需在 i ...
- python-pymysql防止sql注入攻击介绍
目录 pymysql sql 注入攻击 调用存储过程 pymysql pymysql 是一个第三方模块,帮我们封装了 建立表/用户认证/sql的执行/结果的获取 import pymysql # 步骤 ...
- nodeJS从入门到进阶一(基础部分)
一.Node.js基础知识 1.概念 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是JavaScript的运行环境 Node.js 使用了一个事件驱动.非阻塞 ...
- wamp基本配置与设置外网访问
wamp安装(都是一键安装)正常启动后,做一些基本配置的介绍: 1.打开rewrite_module,方法一:左键点击wamp图标,鼠标移至Apache,然后平移至Apache模块,勾选rewrite ...
- jQuery-使页面回到顶部
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 爬取网易云音乐歌手和id
pip install lxml csv requests from lxml import etree from time import sleep import csv import reques ...
- ZMQ应用
一. ZeroMQ概述 ZeroMQ是一种基于消息队列的多线程网络库,其对套接字类型.连接处理.帧.甚至路由的底层细节进行抽象,提供跨越多种传输协议的套接字.ZeroMQ是网络通信中新的一层,介于应 ...
- Redis开发与运维学习笔记
<Redis开发与运维>读书笔记 一.初始Redis 1.Redis特性与优点 速度快.redis所有数据都存放于内存:是用C语言实现,更加贴近硬件:使用了单线程架构,避免了多线程竞争 ...