[LeetCode-150 classic interview questions-day21]

Table of contents

120.Triangular minimum path sum

 64. Minimum path sum

63. Different paths Ⅱ

 5. The longest palindrome substring


 

120.Triangular minimum path sum

Question meaning:

Given a triangle  triangle , find the minimum sum of paths from top to bottom.

Each step can only move to the adjacent node in the next row. Adjacent nodes  here refer to   two nodes whose subscripts are the same as the subscript of the previous layer node  or  equal  to  the subscript of the previous layer node + 1 . That is, if you are at the subscript of the current row   , then the next step can be to move to the subscript of the next row   or   .iii + 1

[Input example] triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]

【Output sample】11

Problem-solving ideas:

1. Define a new two-dimensional list, list[j][i] represents the sum of the minimum paths obtained at the j-th row, where the subscript is i.

2. Calculate while traversing the triangle until the entire triangle is traversed, and output the minimum value of the last row.

3. The rule of dynamic programming is triangle[j][i] += min(list[j-1][i-1],list[j-1][i]), and the initial value is list[0][0 ]=triangle[0][0];

class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        int len = triangle.size();//共有多少行;
        int[][] list = new int[len][len];

        list[0][0] = triangle.get(0).get(0);
        
        for(int j = 1;j < len; ++j){
            list[j][0] = triangle.get(j).get(0)+list[j-1][0];
            for(int i= 1; i < j; ++i){
                //普通情况
                list[j][i] = triangle.get(j).get(i)+Math.min(list[j-1][i-1],list[j-1][i]);
            }
            list[j][j] = triangle.get(j).get(j)+list[j-1][j-1];
        }
        int  result = Integer.MAX_VALUE;
        for(int i = 0;i< len;++i){
            result = Math.min(result,list[len-1][i]);
        }
        return result;
    }
}

Time: Beat 77.67%

Memory: Beaten by 42.52%

 64. Minimum path sum

Question meaning:

Given a   grid  containing non-negative integers  , find a path from the upper left corner to the lower right corner such that the sum of the numbers on the path is minimum.m x ngrid

Note: You can only move one step down or right at a time.

[Input example] grid = [[1,3,1],[1,5,1],[4,2,1]]

[Output sample] 7

Problem-solving ideas:

1. Define a new two-dimensional list, list[i][j] represents the minimum path sum obtained in the j-th column in the i-th row

2. Calculate while traversing the grid until the entire grid is traversed and output list[m-1][n-1].

3. The rule of dynamic programming is list[i][j] = grid[i][j] + Math.min(list[i-1][j],list[i][j-1]);

4. The initialization of dynamic programming can only go down or to the right, then the values ​​of the first row and the first column are fixed, list[0][0] = grid[0][0]; list[0] [j] = list[0][j-1]+grid[0][j]; list[i][0] = list[i-1][0]+grid[i][0]

class Solution {
    public int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int[][] list = new int[m][n];
        list[0][0] = grid[0][0];
        for(int i=1;i<m;++i){
            //第一列赋值
            list[i][0] = list[i-1][0] + grid[i][0];
        }
        for(int j=1;j<n;++j){
            //第一行赋值
            list[0][j] = list[0][j-1] + grid[0][j];
        }
        for(int i = 1; i< m; ++i){
            for(int j = 1; j < n; ++j){
                list[i][j] = grid[i][j] + Math.min(list[i-1][j],list[i][j-1]);
            }
        }
        return list[m-1][n-1];
    }
}

Time: Beat 93.62%

Memory: Beaten by 18.20%

63. Different paths Ⅱ

Question meaning:

A robot is located in  m x n the upper left corner of a grid (the starting point is marked "Start" in the image below).

The robot can only move one step down or to the right at a time. The robot attempts to reach the lower right corner of the grid (labeled "Finish" in the image below).

Now consider that there are obstacles in the grid. So how many different paths will there be from the upper left corner to the lower right corner?

Obstacles and empty locations in the grid are represented by  1 and  respectively 0 .

[Input example]obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]

[Output sample] 2

Problem-solving ideas:

1. Define a new two-dimensional list. list[i][j] represents the number of paths to the j-th column in the i-th row.

2. Calculate while traversing the obstacleGrid until the entire obstacleGrid is traversed and output list[m-1][n-1].

3. The rule of dynamic programming is that when obstacleGrid[i][j] == 0, list[i][j] = list[i-1][j]+list[i][j-1]; number of paths To add up,

If obstacleGrid[i][j]==1, assign list[i][j] to 0

4. Initialization of dynamic programming, list[0][0] = 1; can only go down or to the right, then the values ​​​​of the first row and the first column are fixed. If the first row and the first column do not Obstacles, all are 1, if there are obstacles, everything after the obstacle is 0.

class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        int[][] list = new int[m][n];
        if(obstacleGrid[0][0] == 1 || obstacleGrid[m-1][n-1] == 1){
            return 0;
        }
        list[0][0] = 1;
        
        for(int i=1;i<m && obstacleGrid[i][0] == 0;++i){
            //开始判断第一列的初始值
            list[i][0] = 1;
        }
        for(int j=1;j<n &&  obstacleGrid[0][j] == 0 ;++j){
            //开始判断第一行的初始值
            list[0][j] = 1;
        }
        //开始剩余格子的路径数
        for(int i=1;i < m; ++i){
            for(int j=1; j < n; ++j){
                list[i][j] = obstacleGrid[i][j] == 1 ? 0 : list[i-1][j]+list[i][j-1];
            }
        }
        return list[m-1][n-1];
    }
}

Time: Beat 100.00%

Memory: Beaten by 29.85%

 5. The longest palindrome substring

Question meaning:

Given a string  s, find  s the longest palindrome substring in it.

If the reverse order of a string is the same as the original string, the string is called a palindrome string.

hint:

  • 1 <= s.length <= 1000
  • s Composed only of numbers and English letters

【Input example】s="babad"

[Output sample] "bab" or "aba"

Problem-solving ideas:

This question, well, I can’t figure it out very well, so I refer to the official solution.

I just wrote a few more notes on what I understood.

class Solution {
    public String longestPalindrome(String s) {
        int len = s.length();
        if(len < 2){
            return s;
        }

        int maxLen = 1;
        int begin = 0;
        //dp[i][j]表示s[i..j]是否是回文串
        boolean[][] dp = new boolean[len][len];
        //初始化,所有长度为1的字串都是回文串
        for(int i=0; i<len; ++i){
            dp[i][i] = true;
        }

        char[] charArray = s.toCharArray();
        //先枚举字串长度
        for(int L = 2; L <= len; L++){
            //枚举左边界
            for(int i=0; i< len; ++i){
                //由L和i确定右边界,即j-i+1=L
                int j = L + i - 1;
                //如果右边界越界,就可以退出当前循环
                if(j >= len){
                    break;
                }
                if(charArray[i] != charArray[j]){
                    dp[i][j] = false;
                }else{
                    if(j-i < 3){
                        //这种情况一定是回文串
                        //j-i=0,表示只有一个字符x
                        //j-i=1,两个字符xx,这里已知相等
                        //j-i=2,三个字符,已知最左和最右相等,中间无所谓,即xyx
                        dp[i][j] = true;
                    }else{
                        //此时其是不是回文串,取决于中间部分是不是回文串
                        dp[i][j] = dp[i+1][j-1];
                    }
                }
                if(dp[i][j] && j-i+1>maxLen){
                    maxLen = j-i+1;
                    begin = i;
                }
            }
        }
        return s.substring(begin,begin+maxLen);
    }
}

Time: Defeated 22.23%

Memory: Beaten by 30.02%

Guess you like

Origin blog.csdn.net/qq_37998848/article/details/132662311