1+ Given a rows x cols matrix grid representing a field of cherries .
2+ Each cell in grid represents the number of cherries that you can collect .
3+
4+ You have two robots that can collect cherries for you ,
5+ Robot - 1 is located at the top - left corner (0 ,0 ) ,
6+ and Robot - 2 is located at the top - right corner (0 , cols - 1 ) of the grid .
7+
8+ Return the maximum number of cherries collection using both robots by following the rules below :
9+
10+ From a cell (i ,j ), robots can move to cell (i + 1 , j - 1 ) , (i + 1 , j ) or (i + 1 , j + 1 ).
11+ When any robot is passing through a cell , It picks it up all cherries ,
12+ and the cell becomes an empty cell (0 ).
13+ When both robots stay on the same cell , only one of them takes the cherries .
14+ Both robots cannot move outside of the grid at any moment .
15+ Both robots should reach the bottom row in the grid .
16+
17+
18+ Example 1 :
19+ Input : grid = [[3 ,1 ,1 ],[2 ,5 ,1 ],[1 ,5 ,5 ],[2 ,1 ,1 ]]
20+ Output : 24
21+ Explanation : Path of robot #1 and #2 are described in color green and blue respectively.
22+ Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12.
23+ Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12.
24+ Total of cherries : 12 + 12 = 24.
25+
26+ Example 2 :
27+ Input : grid = [[1 ,0 ,0 ,0 ,0 ,0 ,1 ],[2 ,0 ,0 ,0 ,0 ,3 ,0 ],[2 ,0 ,9 ,0 ,0 ,0 ,0 ],[0 ,3 ,0 ,5 ,4 ,0 ,0 ],[1 ,0 ,2 ,3 ,0 ,0 ,6 ]]
28+ Output : 28
29+ Explanation : Path of robot #1 and #2 are described in color green and blue respectively.
30+ Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17.
31+ Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11.
32+ Total of cherries : 17 + 11 = 28.
33+
34+ Example 3 :
35+ Input : grid = [[1 ,0 ,0 ,3 ],[0 ,0 ,0 ,3 ],[0 ,0 ,3 ,3 ],[9 ,0 ,3 ,3 ]]
36+ Output : 22
37+
38+ Example 4 :
39+ Input : grid = [[1 ,1 ],[1 ,1 ]]
40+ Output : 4
41+
42+
43+
44+ Constraints :
45+ rows == grid .length
46+ cols == grid [i ].length
47+ 2 <= rows , cols <= 70
48+ 0 <= grid [i ][j ] <= 100
49+
50+
51+
52+ # O(mn^2) Time and O(mn^2) Space
53+ class Solution :
54+ def cherryPickup (self , grid : List [List [int ]]) -> int :
55+
56+ @lru_cache (None )
57+ def dfs (row , col_1 , col_2 ):
58+
59+ if dp [row ][col_1 ][col_2 ] != - 1 :
60+
61+ return dp [row ][col_1 ][col_2 ]
62+
63+ if row == m :
64+ return 0
65+
66+
67+ if col_1 == col_2 :
68+
69+ num_cherries = grid [row ][col_1 ]
70+
71+ else :
72+
73+ num_cherries = grid [row ][col_1 ] + grid [row ][col_2 ]
74+
75+
76+ next_row_result = 0
77+
78+
79+ for i in [- 1 , 0 , 1 ]:
80+ for j in [- 1 , 0 , 1 ]:
81+
82+ next_col_1 = col_1 + i
83+ next_col_2 = col_2 + j
84+
85+ if 0 <= next_col_1 < n and 0 <= next_col_2 < n :
86+
87+ next_row_result = max (next_row_result , dfs (row + 1 , next_col_1 , next_col_2 ))
88+
89+ answer = num_cherries + next_row_result
90+
91+ dp [row ][col_1 ][col_2 ] = answer
92+
93+ return answer
94+
95+
96+ dp = [[[- 1 for i in range (70 )] for j in range (70 )] for k in range (70 )]
97+
98+
99+ m = len (grid )
100+ n = len (grid [0 ])
101+
102+ return dfs (0 , 0 , n - 1 )
0 commit comments