Dynamic programming - the maximum value of gifts (reproduced)

1. Problem-solving ideas of dynamic programming

  1. Decompose the original problem into sub-problems. Decompose the original problem into several sub-problems. The sub-problems
    are the same or similar to the original problem, but the scale becomes smaller.
    solution once
  2. Determining the state
    When solving problems with dynamic programming, we often call a set of values ​​​​of various variables related to sub-problems a "state". A "state" corresponds to one or more sub-problems, and the so-called "value" under a certain "state" is the solution to the sub-problem corresponding to this "state".
  3. Determine the values ​​of some initial states (boundary states)
  4. Determining the state transition equation

2 Problem-solving ideas

2.1 Question stem

There is a gift on each grid of an m*n chessboard, and each gift has a certain value (value greater than 0). You can start taking the gift in the grid from the upper left corner of the board, and move right or down one grid at a time until you reach the lower right corner of the board. Given a chessboard and the value of gifts on it, please calculate how much value of gifts you can get at most?

class Solution {
    
    
    public int maxValue(int[][] grid) {
    
    
        int m = grid.length, n = grid[0].length;
        for(int i = 0;i <m ;i++){
    
    
            for(int j = 0;j<n;j++){
    
    
                if(i == 0 && j == 0) continue;
                if(i == 0 ) grid[i][j] += grid[i][j-1];
                else if(j == 0) grid[i][j] += grid[i-1][j];
                else grid[i][j] += Math.max(grid[i][j-1],grid[i-1][j]);
            }
            
        }
        return grid[m-1][n-1];

    }
}

According to the description of the topic, it is easy to get a certain cell only from the upper cell or the left cell.

Let f(i, j) be the maximum cumulative value of the gift from the upper left corner of the chessboard to the cell (i, j), and it is easy to get the following recurrence relation: f(i, j) f(i, j) is equal to f(i ,j-1) and the greater value of f(i-1,j) plus the current cell gift value grid(i,j)

f(i,j) = max[f(i,j-1), f(i-1,j)] + grid(i,j)
f(i,j)=max[f(i,j−1),f(i−1,j)]+grid(i,j)

Therefore, dynamic programming can be used to solve this problem, and the above formula is the transfer equation.

State definition: Let the dynamic programming matrix dp, dp(i,j) represent the maximum cumulative value of gifts that can be obtained when reaching the cell (i,j)(i,j) starting from the upper left corner of the chessboard.
insert image description here

3. Other topics in dynamic programming

3.1 Translate numbers into strings

Given a number, we translate it into a string according to the following rules: 0 is translated into "a", 1 is translated into "b", ..., 11 is translated into "l", ..., 25 is translated into "z". A number may have multiple translations. Please program a function to calculate how many different translation methods there are for a number.

class Solution {
    
    
    public int translateNum(int num) {
    
    
        char[] ch =String.valueOf(num).toCharArray();
        int len = ch.length;
        int[] dp = new int[len+1];
        dp[0] = 1;
        dp[1] = 1;
        for(int i =2;i<= len;i++){
    
    
            int n = (ch[i-2] -'0')*10 +(ch[i-1]-'0');
            if(n>= 10 && n<=25){
    
    
                dp[i] = dp[i-1] +dp[i-2];
            }else{
    
    
                dp[i] =dp[i-1];
            }
        }
        return dp[len];
    }
}

3.2 Ugly numbers

We call a number containing only prime factors 2, 3 and 5 an ugly number (Ugly Number). Find the nth ugly number in ascending order.

class Solution {
    
    
    public int nthUglyNumber(int n) {
    
    
        int a =0,b=0,c =0;
        int[] dp  = new int[n];
        dp[0] = 1;
        for(int i = 1;i<n ;i++){
    
    
            int n2 = dp[a]* 2,n3 =dp[b]*3,n5=dp[c]*5;
            dp[i] =Math.min(Math.min(n2,n3),n5);
            if(dp[i] == n2) a++;
            if(dp[i] == n3) b++;
            if(dp[i] == n5) c++;
        }
        return dp[n-1];
    }
}

Author: jyd
Contact:
https://leetcode-cn.com/problems/li-wu-de-zui-da-jie-zhi-lcof/solution/mian-shi-ti-47-li-wu-de- zui-da-jie-zhi-dong-tai-gu/
Origin: LeetCode

Guess you like

Origin blog.csdn.net/qq_21561833/article/details/122776327