[Interview Assault Algorithm Day Three] Sword Pointer Offer + Leetcode Hot100

The Bright Sword Project was officially launched on June 25, 2022. Until the beginning of August, 5 algorithm questions will be reviewed every day. The questions I chose are Sword Pointer Offer and LeetCodeHot100, because these questions are basically common interview questions, and you can do more before the interview. Just look at the interview experience and familiarize yourself with the algorithm questions that each company has tested.

Sword Points Offer 13. Robot's range of motion

  • Question meaning: There is a square grid with m rows and n columns on the ground, from coordinates [0,0] to coordinates [m-1,n-1]. A robot starts moving from the grid at coordinates [0, 0]. It can move one grid to the left, right, up, or down each time (it cannot move outside the grid), and it cannot enter a space where the sum of row coordinates and column coordinates is greater than k grid. For example, when k is 18, the robot can enter square [35, 37] because 3+5+3+7=18. But it cannot enter square [35, 38], because 3+5+3+8=19. How many grids can this robot reach?
  • Example:
    输入:m = 2, n = 3, k = 1
    输出:3
    
  • Idea: Set the template of the island path. This question can 剑指offer12be compared with. During the search process, that question needs to be retraced during the backtracking process visited[i][j]=false, because we are starting from any point again, and this question stipulates that from (0,0 ) point, so as long as you pass it, you will not go a second time, so there is no need to set it to false. The key point is this
  • Code:
    class Solution {
          
          
        public int movingCount(int m, int n, int k) {
          
          
            boolean[][] visited = new boolean[m][n];
            return dfs(m, n, k, visited, 0, 0);
        } 
        private int dfs(int m, int n, int k, boolean[][] visited, int i, int j) {
          
          
            if (i < 0 || i >= m || j < 0 || j >= n || visited[i][j])
                return 0;
            if (getSum(i) + getSum(j) > k)
                return 0;
            visited[i][j] = true;
            return dfs(m, n, k, visited, i - 1, j) +
                    dfs(m, n, k, visited, i + 1, j) +
                    dfs(m, n, k, visited, i, j - 1) +
                    dfs(m, n, k, visited, i, j + 1) + 1;
        }
        private int getSum(int a) {
          
          
            int sum = 0;
            while (a != 0) {
          
          
                sum += a % 10;
                a /= 10;
            }
            return sum;
        }
    }
    

Sword Points Offer 14- I. Cut the rope

  • Question: You are given a rope of length n. Please cut the rope into m segments of integer length (m and n are integers, n>1 and m>1). The length of each segment of the rope is recorded as k[0 ],k[1]…k[m-1] . What is the maximum possible product of k[0] k[1] ...*k[m-1]? For example, when the length of the rope is 8, we cut it into three sections with lengths 2, 3, and 3 respectively. The maximum product obtained at this time is 18.
  • Example:
    输入: 2
    输出: 1
    解释: 2 = 1 + 1, 1 × 1 = 1
    
  • Idea:
  • Code:

Guess you like

Origin blog.csdn.net/qq_42397330/article/details/125492882