• Customer Support: admin@thealgorists.com
  • Feedback: We are listening to your every feedback, and taking action to constantly improve your learning experience. If you have any feedback, please use this form: https://thealgorists.com/Feedback.
  • If you are a student, email to admin@thealgorists.com to get special student discount.



The DP problems belonging to this category, in its simplest form, looks like below or some kind of variations of it:
  • Given a target find minimum (maximum) cost / path / sum to reach the target.


The solution for this kind of problems, in a very generalized form, would often look like below:
  • Choose optimal (minimal or maximal, as the case may be) path among all possible paths that lead to the current state, and then add value for the current state.
  • routes[curr] = min(routes[curr - 1], routes[curr - 2], ... , routes[curr - k]) + cost[i]

    where current target can be reached only from (curr - 1), (curr - 2), ... (curr - k).
  • Overall the solution would look like this :
    
    for (int curr = 1; curr <= target; curr++) {
       for (int k = 0; k < waysToReachCurrentTarget.size(); k++) {
        dp[i] = min(dp[curr], dp[waysToReachCurrentTarget[k]] + cost / path ) ;
       }
    }
    
    return dp[target];
    
    


The below problem and its solution beautifully demonstrate this approach:

Problem Statement:


Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
  [1,3,1],
  [1,5,1],
  [4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.


Solution:


class Solution {
    // 2-D Dynamic Programming
    public int minPathSum(int[][] grid) {
        if (grid == null || grid.length == 0) {
            return 0;
        }
        int m = grid.length;
        int n = grid[0].length;
        int[][] dp = new int[m][n];
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                dp[i][j] = Integer.MAX_VALUE;
                if (i == 0 && j == 0) {
                    dp[i][j] = grid[i][j];
                } else {
                    if (i - 1 >= 0) {
                        dp[i][j] = dp[i - 1][j] + grid[i][j];
                    }
                    if (j - 1 >= 0) {
                        dp[i][j] = Math.min(dp[i][j], dp[i][j - 1] + grid[i][j]);
                    }
                }
            }
        }
        return dp[m - 1][n - 1];
    }
}



Naive Solution:


int minCost(int cost[R][C], int m, int n)
{
   if (n < 0 || m < 0)
      return INT_MAX;
   else if (m == 0 && n == 0)
      return cost[m][n];
   else
      return cost[m][n] + min( minCost(cost, m-1, n-1),
                               minCost(cost, m-1, n), 
                               minCost(cost, m, n-1) );
}



Problem Solving:



The above content is written by:

Abhishek Dey

Abhishek Dey

A Visionary Software Engineer With A Mission To Empower Every Person & Every Organization On The Planet To Achieve More

Microsoft | University of Florida

View LinkedIn profile


If you have any feedback, please use this form: https://thealgorists.com/Feedback.




Subscribe to Our Youtube Channel

Follow Us On LinkedIn
wave