You are given an m x n matrix grid consisting of positive integers. You can move from a cell in the matrix to any other cell that is either to the bottom or to the right (not necessarily adjacent). The score of a move from a cell with the value c1 to a cell with the value c2 is c2 - c1.You can start at any cell, and you have to make at least one move.Return the maximum total score you can achieve. Example 1:Input: grid = [[9,5,7,3],[8,9,6,1],[6,7,14,3],[2,5,3,1]]Output: 9Explanation: We start at the cell (0, 1), and we perform the following moves:- Move from the cell (0, 1) to (2, 1) with a score of 7 - 5 = 2.- Move from the cell (2, 1) to (2, 2) with a score of 14 - 7 = 7.The total score is 2 + 7 = 9.Example 2:Input: grid = [[4,3,2],[3,2,1]]Output: -1Explanation: We start at the cell (0, 0), and we perform one move: (0, 0) to (0, 1). The score is 3 - 4 = -1. Constraints:m == grid.lengthn == grid[i].length2 <= m, n <= 10004 <= m * n <= 1051 <= grid[i][j] <= 105
Question
You are given an m x n matrix grid consisting of positive integers. You can move from a cell in the matrix to any other cell that is either to the bottom or to the right (not necessarily adjacent). The score of a move from a cell with the value c1 to a cell with the value c2 is c2 - c1.You can start at any cell, and you have to make at least one move.Return the maximum total score you can achieve. Example 1:Input: grid = [[9,5,7,3],[8,9,6,1],[6,7,14,3],[2,5,3,1]]Output: 9Explanation: We start at the cell (0, 1), and we perform the following moves:- Move from the cell (0, 1) to (2, 1) with a score of 7 - 5 = 2.- Move from the cell (2, 1) to (2, 2) with a score of 14 - 7 = 7.The total score is 2 + 7 = 9.Example 2:Input: grid = [[4,3,2],[3,2,1]]Output: -1Explanation: We start at the cell (0, 0), and we perform one move: (0, 0) to (0, 1). The score is 3 - 4 = -1. Constraints:m == grid.lengthn == grid[i].length2 <= m, n <= 10004 <= m * n <= 1051 <= grid[i][j] <= 105
Solution
This problem can be solved using dynamic programming. Here are the steps to solve this problem:
-
Initialize a 2D DP array with the same dimensions as the input grid. This DP array will store the maximum score we can achieve at each cell.
-
Iterate over the grid from bottom to right. For each cell, calculate the maximum score we can achieve by moving to this cell from its top cell or its left cell. The score of moving to a cell is the value of the destination cell minus the value of the source cell. If the score is negative, we ignore this move because it decreases our total score.
-
Update the DP array with the maximum score we can achieve at each cell.
-
The maximum total score we can achieve is the maximum value in the DP array.
Here is a Python solution for this problem:
def maxScore(grid):
m, n = len(grid), len(grid[0])
dp = [[0]*n for _ in range(m)]
for i in range(m-1, -1, -1):
for j in range(n-1, -1, -1):
if i < m-1:
dp[i][j] = max(dp[i][j], max(0, dp[i+1][j] + grid[i+1][j] - grid[i][j]))
if j < n-1:
dp[i][j] = max(dp[i][j], max(0, dp[i][j+1] + grid[i][j+1] - grid[i][j]))
return max(max(row) for row in dp)
This solution works in O(mn) time and uses O(mn) space.
Similar Questions
Max Ones in a RowMax Score: 20Given a M x N binary matrix, find the 0-indexed position of the row that contains the maximum count of ones, and the number of ones in that row. In case there are multiple rows that have the maximum count of ones, the row with the smallest row number should be selected.Input FormatThe first line of input contains integers M and N separated by space. For the next M lines, each line contains N elements separated by spaces.Output FormatPrint the index of the row, followed by the number of ones in it.Constraints1 ≤ M, N ≤ 100ExampleInput
There is an m x n grid with a ball. The ball is initially at the position [startRow, startColumn]. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply at most maxMove moves to the ball.Given the five integers m, n, maxMove, startRow, startColumn, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it modulo 109 + 7. Example 1:Input: m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0Output: 6
You are given a 2D matrix grid of size m x n. You need to check if each cell grid[i][j] is:Equal to the cell below it, i.e. grid[i][j] == grid[i + 1][j] (if it exists).Different from the cell to its right, i.e. grid[i][j] != grid[i][j + 1] (if it exists).Return true if all the cells satisfy these conditions, otherwise, return false. Example 1:Input: grid = [[1,0,2],[1,0,2]]Output: trueExplanation:All the cells in the grid satisfy the conditions.Example 2:Input: grid = [[1,1,1],[0,0,0]]Output: falseExplanation:All cells in the first row are equal.Example 3:Input: grid = [[1],[2],[3]]Output: falseExplanation:Cells in the first column have different values. Constraints:1 <= n, m <= 100 <= grid[i][j] <= 9
n = len(grid) x = sum(max(grid[i][j] for i in range(n)) for j in range(n)) y = sum(max(grid[j][i] for i in range(n)) for j in range(n)) z = sum(grid[i][j] > 0 for i in range(n) for j in range(n)) return sum([x,y,z])
You are given a 0-indexed integer array nums and an integer k. You have a starting score of 0.In one operation:choose an index i such that 0 <= i < nums.length,increase your score by nums[i], andreplace nums[i] with ceil(nums[i] / 3).Return the maximum possible score you can attain after applying exactly k operations.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.