Skip to content

Commit bb992ae

Browse files
committed
Day70
1 parent d7bf4e5 commit bb992ae

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Day70/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {number}
4+
*/
5+
var cherryPickup = function (grid) {
6+
7+
let r = grid.length;
8+
let c = grid[0].length;
9+
10+
let map = {};
11+
12+
function helper(row, col1, col2) {
13+
//out of bound
14+
if (row >= r || col1 < 0 || col1 >= c || col2 < 0 || col2 >= c) {
15+
return 0
16+
}
17+
let cherry = grid[row][col1] + (col1 == col2 ? 0 : grid[row][col2]);
18+
19+
let key = `${row}-${col1}-${col2}`;
20+
21+
if ((key in map)) return map[key]
22+
23+
let max = 0;
24+
25+
for (let i = -1; i <= 1; i++) {
26+
for (let j = -1; j <= 1; j++) {
27+
max = Math.max(max, helper(row + 1, col1 + i, col2 + j))
28+
}
29+
}
30+
31+
return map[key] = max + cherry
32+
33+
}
34+
35+
return helper(0, 0, c - 1)
36+
};

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,6 @@ If you are loving solving problems in leetcode, please contact me to enjoy it to
156156
|Day 68| [454. 4Sum II](https://leetcode.com/problems/4sum-ii/) | [javascript]()|[:memo:](https://leetcode.com/problems/4sum-ii/)|Medium|
157157

158158

159-
|Day 69| [334. Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [javascript]()|[:memo:](https://leetcode.com/problems/increasing-triplet-subsequence/)|Medium|
159+
|Day 69| [334. Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [javascript]()|[:memo:](https://leetcode.com/problems/increasing-triplet-subsequence/)|Medium|
160+
161+
|Day 70| [1463. Cherry Pickup II](https://leetcode.com/problems/cherry-pickup-ii/submissions/) | [javascript]()|[:memo:](https://leetcode.com/problems/cherry-pickup-ii/submissions/)|Hard|

0 commit comments

Comments
 (0)