![]() |
VOOZH | about |
Given a m*n grid with each cell consisting of positive, negative, or no points i.e., zero points. From a cell (i, j) we can move to (i+1, j) or (i, j+1) and we can move to a cell only if we have positive points ( > 0 ) when we move to that cell. Whenever we pass through a cell, points in that cell are added to our overall points. The task is to find the minimum initial points to reach cell (m-1, n-1) from (0, 0).
Example:
Input: points[][] = [[-2, -3, 3], [-5,-10, 1], [10, 30, -5]]
Output: 7
Explanation: 7 is the minimum value to reach the destination with positive throughout the path. Below is the path (0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2). We start from (0, 0) with 7, we reach (0, 1) with 5, (0, 2) with 2, (1, 2) with 5, (2, 2) with and finally we have 1 point (we needed greater than 0 points at the end).Input: points[][] = [[2, 3], [5, 10], [10,30]]
Output: 1
Explanation: Take any path, all of them are positive. So, required one point at the start
Table of Content
For any cell (i, j), we recursively find the minimum initial points needed for its right and down neighbors (i.e., (i, j+1) and (i+1, j)). The minimum of these values, minExitPoints, determines the number of points needed at the current cell:
- If points[i][j] > 0, the minimum points needed are max(minExitPoints - points[i][j], 1).
- If points[i][j] <= 0, we need at least minExitPoints - points[i][j] to ensure we can move forward.
Mathematically the recurrence relation will look like the following:
minPoints(i, j) = max(minExitPoints - points[i][j], 1) where minExitPoints = min(minPoints[i+1][j], minPoints[i][j+1]).
Base case: If the destination cell is reached (i == m-1 && j == n-1), we compute the minimum points needed:
- If the cell value is positive, return 1.
- If the cell value is negative or zero, return abs(points[i][j]) + 1 to ensure that we have enough points to end up with positive points.
Out-of-bounds cells: For any out-of-bounds cell (i.e., when i < 0 or j < 0), we return INT_MAX as it represents an invalid path.
How the above expression covers all the cases?
7
If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:
1. Optimal Substructure: Minimum points required at cell (i, j) depends on the optimal solutions of minPoints(i+1, j) and minPoints(i, j+1). By comparing these optimal substructures, we can efficiently calculate minPoints(i, j).
2. Overlapping Subproblems: While applying a recursive approach in this problem, we notice that certain subproblems are computed multiple times. For example, minPoints(1,1) will call minPoints(1, 2) and minPoints(2, 1). Both minPoints(1, 2) and minPoints(2, 1) will call minPoints(2, 2).
7
The idea is to use a 2D dp array where dp[i][j] represents the minimum initial points required to reach destination cell (m-1, n-1) starting from cell (i, j). From any cell (i, j) there two options to move (i+1, j) and (i, j+1), so to compute dp[i][j], we choose a cell that require less intial points to reach the end i.e., min(dp[i+1][j], dp[i][j+1]).
Follow the steps to solve the problem
7
If we take a closer look at the problem, we can notice that this is more of a graph problem where we have two outgoing edges from every point. The edges can have negative weight, but there are no back edges. So we can use Dijkstra's Shortest Path Algorithm here and since there are no back edges, we do not have to keep track of the visited elements. We can simply use a priority queue to pick the next vertex like we do in Dijkstra's algorithm.
7