Sword Finger Offer Interview Question 47: The Maximum Value of Gifts (Dynamic Programming)

This question is rather confusing at first, because I just did a question that uses a one-dimensional array to record the value. If this question is to select the largest element from the front to the back and add it to the path, then the size of the next element will still affect the total size. The results cannot be guaranteed.

However, if a two-dimensional array is used to record the maximum value of the path from i to j, and the value of node i, j is equal to the maximum value of the upper and left path maximums, therefore, starting from the upper left corner, you can Get the maximum value of all paths,

Return the value in the lower right corner and that's it.

In this question, the result is obtained from front to back. There is no bottom-up like the previous question, because the recurrence formula of this question is not i=i[count + 1] +... Seeking the current element does not need to know the subsequent elements in advance

My code

 

    int[][] p = null;
    public int maxValue(int[][] grid) {
        p = new int[grid.length][grid[0].length];
        for(int count_1 = 0; count_1<p.length ;count_1++){
            for(int count_2 = 0; count_2<p[0].length ;count_2++){
                p[count_1][count_2] = 0;
            }
        }
        int i = 0;
        int j = 0;
        while(i<p.length&&j<p[0].length){
            for(int count_2 = j;count_2<p[0].length;count_2++){
                findMax(grid,i,count_2);
            }
            for(int count_1 = i;count_1<p.length;count_1++){
                findMax(grid,count_1,j);
            }
            i++;
            j++;

        }
        

        return p[p.length-1][p[0].length-1];
    }
    public void findMax(int[][] grid,int i,int j){
        if(i==0&&j==0){
            p[i][j] = grid[i][j];
        }else if(i==0){
            p[i][j] = p[i][j-1] + grid[i][j];
        }else if(j==0){
            p[i][j] = p[i-1][j] + grid[i][j];
        }else{
            p[i][j] = p[i][j-1] > p[i-1][j]? p[i][j-1] + grid[i][j]:p[i-1][j] + grid[i][j];
        }
    }

Optimization

This method uses a one-dimensional array. As the array is updated one by one, the value of the current row that has been updated before j is the value of the previous row that has not been updated after j and j.

    int[] max_val = new int[p[0].length];
    public int maxValue(int[][] grid) {
        p = new int[grid.length][grid[0].length];
        for(int count_1 = 0; count_1<p.length ;count_1++){
            for(int count_2 = 0; count_2<p[0].length ;count_2++){
                p[count_1][count_2] = 0;
            }
        }
        int i = 0;
        int j = 0;
        
            
            for(int count_1 = i;count_1<p.length;count_1++){
                for(int count_2 = j;count_2<p[0].length;count_2++){
                    findMax(grid,i,count_2);
                }
            }
            i++;
            j++;

        }
        

        return p[p.length-1][p[0].length-1];
    }
    public void findMax(int[][] grid,int i,int j){
        if(i==0&&j==0){
            max_val[j] = grid[i][j];
        }else if(i==0){
            max_val[j] = max_val[j-1] + grid[i][j];
        }else if(j==0){
            max_val[j] = max_val[j] + grid[i][j];
        }else{
            max_val[j] = max_val[j] > max_val[j-1]? max_val[j] + grid[i][j]:max_val[j-1] + grid[i][j];
        }
    }

 

 

Guess you like

Origin blog.csdn.net/qq_40473204/article/details/114579872