VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-positive-points-to-reach-destination/

⇱ Minimum Initial Points to Reach Destination - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum Initial Points to Reach Destination

Last Updated : 23 Jul, 2025

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

Using Recursion - O(2^(m*n)) Time and O(m*n) Space

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?

  1. If points[i][j] == 0, No additional points are needed; we leave with the same points as we entered.
  2. If points[i][j] < 0, To account for the negative value, we need enough points to compensate the loss, i.e., minExitPoints - points[i][j].
  3. If points[i][j] > 0, We can enter with as few points as minExitPoints - points[i][j], but if this value drops to 0 or below, we must enter with at least 1 point. Therefore, the formula max(minExitPoints - points[i][j], 1) ensures that the points remain positive.

Output
7

Using Top-Down DP (Memoization) - O(n*m) Time and O(n*m) Space

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).

  • There are only are two parameters: i and j that changes in the recursive solution. So we create a 2D matrix of size m*n for memoization.
  • We initialize this matrix as -1 to indicate nothing is computed initially.
  • Now we modify our recursive solution to first check if the value is -1, then only make recursive calls. This way, we avoid re-computations of the same subproblems.

Output
7

Using Bottom-Up DP (Tabulation) - O(m*n) Time and O(m*n) Space

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

  • Create a 2D dp array of the same size as the grid, where dp[i][j] represents the minimum initial points required to reach destination cell (m-1, n-1) starting from cell (i, j). dp[0][0] is our final answer.
  • Start filling the array from the bottom right corner to left top.
  • At any cell (i, j) there two options to move (i-1, j) and (i, j-1). We choose a cell that require less initial points to reach the end i.e., minExitPoints = min(dp[i-1][j], dp[i][j-1]). From minExitPoints we can compute dp[i][j] by: dp[i][j] = max(minExitPoints - points[i][j], 1)

Output
7

Using Dijkstra's Shortest Path Algorithm - O((m*n)*log(m*n)) Time and O(m*n) Space

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.


Output
7
Comment