当前位置:网站首页>Leetcode memory deep search / dynamic planning V2
Leetcode memory deep search / dynamic planning V2
2022-07-24 18:31:00 【Linchong Fengxue mountain temple】
741. Pick cherries


My wrong solution
class Solution(object):
def cherryPickup(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
# Two people pick cherries from the starting point at the same time
def dfs(x1,y1,x2,y2):
if x1 >= len(grid) or y1 >= len(grid) or x2 >= len(grid) or y2 >= len(grid):
return float("-inf")
if grid[x1][y1] == -1 or grid[x2][y2] == -1:
return float("-inf")
if (x1,y1) == (x2,y2):
res = grid[x1][y1]
else:
res = grid[x1][y1] + grid[x2][y2]
res += max(dfs(x1+1,y1,x2,y2),dfs(x1,y1+1,x2,y2))
res += max(dfs(x1,y1,x2+1,y2),dfs(x1,y1,x2,y2+1))
return res
return dfs(0,0,0,0)class Solution:
def cherryPickup(self, grid: List[List[int]]) -> int:
@lru_cache(None)
def dfs(x1,y1,x2,y2):
if x1 > len(grid)-1 or x2 > len(grid)-1 or y1 > len(grid[0])-1 or y2 > len(grid[0])-1:
return float('-inf')
if grid[x1][y1] == -1 or grid[x2][y2] == -1:
return float('-inf')
# # To the end point
if x1 == y1 == x2 == y2 == len(grid)-1:
return grid[x1][y1]
res = 0
if (x1,y1) == (x2,y2):
res += grid[x1][y1]
else:
res += grid[x1][y1] + grid[x2][y2]
# Find the largest one from the moving direction of the two points , Two people move at the same time
res += 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))
return res
res = dfs(0,0,0,0)
if res == float("-inf"):
return 0
return res174. Dungeons game


边栏推荐
- L4L7负载均衡
- 【校验】只能输入数字(正负数)
- middleware
- MySQL -- implicit conversion of data type
- Show or hide password plaintext + password box verification information
- Tree chain partition board
- CF lomsat gelral (heuristic merge)
- 7. Character coding?
- Get familiar with pytoch and pytoch environment configuration
- Ionic4 Learning Notes 6 -- using native ionic4 components in custom components
猜你喜欢

Ionic4 learning notes 4 -- add a tab page
![[opencv] - thresholding](/img/4e/88c8c8063de7cb10e44e76e77dbb8e.png)
[opencv] - thresholding

Inoic4 learning notes 2

About the writing method of interface 1 chain interpretation 2. Method execution (finally) must be executed

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag

Oracle EBS form common objects and their relationships

Get familiar with pytoch and pytoch environment configuration

MySQL - bufferpool related information

How to solve the problem that yaml in idea is unrecognized or red?

Getting started with MySQL database
随机推荐
8. = = and = = =?
数组常用方法(2)
CF Lomsat gelral(启发式合并)
Framework introduction
["code" power is fully open, and "chapter" shows strength] list of contributors to the task challenge in the first quarter of 2022
Common methods of string (2)
全国职业院校技能大赛网络安全竞赛——Apache安全配置详解
Problems needing attention in writing pages
Valentine's Day gift ----- use her photos and our chat records to generate word clouds~
undefined reference to H5PTopen
Highcharts chart and report display, export data
A practical scheme of realizing 0.5px on mobile terminal
redis集群的三种方式
树链剖分板子
ORM introduction and database operation
Read zepto source code touch module
L4l7 load balancing
【校验】只能输入数字(正负数)
Array object methods commonly used traversal methods & higher-order functions
pinia 入门及使用