Cherry pickup leetcode

  1. 花花酱 LeetCode 1463. Cherry Pickup II
  2. 741. Cherry Pickup – Programming Interview Questions
  3. LeetFree


Download: Cherry pickup leetcode
Size: 25.66 MB

Algorithm

741. Cherry Pickup Question 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: • grid is an N by N 2D array, with 1 <= N <= 50. • Each grid[i][j] is an integer in the set . • It is guaranteed that grid[0][0] and grid[N-1][N-1] are not -1. Solution: • Method 1: dp • dp[x1][y1][x2]: the max cherry can p...

花花酱 LeetCode 1463. Cherry Pickup II

Given a rows x colsmatrix gridrepresenting a field of cherries.Each cell in gridrepresents the number of cherries that you can collect. You have tworobots that can collect cherries for you, Robot #1 is located at the top-left corner (0,0) , and Robot #2 is located at the top-right corner (0, cols-1) of the grid. Return the maximum number of cherries collection using both robots by following the rules below: • From a cell (i,j), robots can move to cell (i+1, j-1) , (i+1, j) or (i+1, j+1). • When any robot is passing through a cell, It picks it up all cherries, and the cell becomes an empty cell (0). • When both robots stay on the same cell, only one of them takes the cherries. • Both robots cannot move outside of the grid atany moment. • Both robots should reach the bottom row in the grid. Example 1: Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] Output: 24 Explanation:Path of robot #1 and #2 are described in color green and blue respectively. Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12. Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. Total of cherries: 12 + 12 = 24. Example 2: 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]] Output: 28 Explanation:Path of robot #1 and #2 are described in color green and blue respectively. Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17. Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11. Total of cherries: 17 + 11 = 28. Example 3: Input: grid = [[1,0,0,3],[0,0,0,3],[0,0,3,3],[9,0,3,3]] ...

741. Cherry Pickup – Programming Interview Questions

You are given an n x n gridrepresenting a field of cherries, each cell is one of three possible integers. • 0means the cell is empty, so you can pass through, • 1means the cell contains a cherry that you can pick up and pass through, or • -1means the cell contains a thorn that blocks your way. Return the maximum number of cherries you can collect 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 0or 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. Example 2: Input: grid = [[1,1,-1],[1,-1,1],[-1,1,1]] Output: 0 Constraints: • n == grid.length • n == grid[i].length • 1 int: n = len(grid) memo = {} def helper(x1, y1, x2): y2 = (x1 + y1) - x2 if (x1 < 0 or y1 < 0 or x2 < 0 or y2 < 0 or grid[x1][y1] ==...

LeetFree

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:• grid is an N by N 2D array, with 1 <= N <= 50. • Each grid[i][j] is an integer in the set . • It is guaranteed that grid[0][0] and grid[N-1][N-1] are not -1. • • • • Approach #1: Greedy [Wrong Answer] Intuition Let's find the most cherries we can pic...