Summary of leetcode's simulated problem brushing 3

Posted by Kathy on Fri, 25 Feb 2022 15:05:33 +0100

Summary of leetcode's simulated problem brushing 3

1-transpose matrix
Title Link: Title Link stamp here!!!

Idea: just put the value of the corresponding row in the corresponding column. Do an easy question first to comfort yourself.

class Solution {
    public int[][] transpose(int[][] matrix) {
        int m = matrix.length ;
        int n = matrix[0].length ;
        int [][] ans = new int [n][m] ;
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                ans[j][i] = matrix[i][j] ;
            }
        }
        return ans ;
    }
}


2-output match pairs
Title Link: Title Link stamp here!!!

Idea: team[i-1] refers to the initial situation of each team. It needs to go through the pairing of the first layer of cycle times. For each round of pairing, the first and last pairing, the second and penultimate pairing, and so on.

class Solution {
    public String findContestMatch(int n) {
        String [] team = new String[n] ;
        for(int i=1; i<=n; i++){
            team[i-1] = i + "" ;
        }
        for(;n>1;n/=2){
            for(int i=0; i<n/2; i++){
                team[i] = "(" + team[i] + "," + team[n-i-1] + ")" ;
            }
        }
        return team[0] ;
    }
}


3 - Flip image
Title Link: Title Link stamp here!!!

Idea: image [i] [J]= Image [i] [- 1-J], such as: 0, 1, first exchange into 1, 0, then take non and change back to 0, 1, so do nothing.
image[i][j] == image[i][n-1-j], and take non at the same time.

class Solution {
    public int[][] flipAndInvertImage(int[][] image) {
        int m = image.length;
        int n = image[0].length ;
   
        for(int i=0; i<m; i++){
            for(int j=0; j<(n%2==0 ? n/2 : n/2+1); j++){
                if(image[i][j]==image[i][n-j-1]){
                    if(j!=n-j-1){
                        image[i][j] = 1 - image[i][j] ;
                        image[i][n-j-1] = 1 - image[i][n-j-1] ;
                    }else{
                        image[i][j] = 1 - image[i][j] ;
                    }
                }
            }
        }
        return image ;
    }
}


4 - Robots trapped in the ring
Title Link: Title Link stamp here!!!

Idea: (x,y) simulate the current coordinates, face simulates the current face direction, and after a round of instructions, only those who are not at the origin and the face direction has not changed can not finally return to the origin.

class Solution {
    public boolean isRobotBounded(String instructions) {
        int x = 0, y = 0 ; 
        int face = 0 ;
        int [] dx = {-1,0,1,0} ;
        int [] dy = {0,1,0,-1} ;
        char [] c = instructions.toCharArray() ;
        for(char ch : c){
            switch(ch){
                case 'R' : face = (face+1)%4 ; break ;
                case 'L' : face = (face+3)%4 ; break ;
                case 'G' : x += dx[face] ; y += dy[face]; break ;    
            }
        }
        return (x==0 && y==0) || (face % 4 != 0) ;
    }
}


5-cent candy II
Title Link: Title Link stamp here!!!

Idea: just simulate the process of dividing candy. As long as there is candy, you can continue to divide. If the number of candy each time is greater than or equal to the number n that should be divided, divide n. otherwise, divide all the remaining candy.

class Solution {
    public int[] distributeCandies(int candies, int num_people) {
        int [] ans = new int [num_people] ;
        int n = 1 ;
        int i = 0 ;
        while(candies>0){
            if(candies-n>=0){
            ans[i] += n ;
            candies -= n ; 
            n++ ;
            i = (i+1)%num_people ;
        }else{
            ans[i] += candies ;
            break ;
        }
        }
        return ans ;
    }
}


6 - query keyed arrangement
Title Link: Title Link stamp here!!!

Idea: use the List set to simulate the changes of the elements of the array p, delete the corresponding elements from the List set and insert them into the head of the List set each time.

class Solution {

    public int[] processQueries(int[] queries, int m) {
        List<Integer> p = new ArrayList<>() ;
        for(int i=0; i<m; i++){
            p.add(i+1) ;
        }
        int [] ans = new int [queries.length] ;
        for(int i=0; i<queries.length; i++){
            int x = f(queries[i],p) ;
            ans[i] = x ;
            p.remove(x) ;
            p.add(0,queries[i]) ;
        } 
        return ans ;
    }
    public int f(int value, List<Integer> p){
        for(int i=0; i<p.size(); i++){
            if(value==p.get(i)){
                return i ;
            }
        }
        return 0  ;
    }
}


7 - number of odd value cells
Title Link: Title Link stamp here!!!

Idea: use two arrays row and column to mark whether rows and columns are odd or not.

class Solution {
    public int oddCells(int m, int n, int[][] indices) {
        boolean [] row = new boolean[m] ;
        boolean [] column = new boolean[n] ;
        int r = 0, c = 0 ;
        for(int i=0; i<indices.length; i++){
            row[indices[i][0]] = !row[indices[i][0]] ;
            column[indices[i][1]] = !column[indices[i][1]] ;
        }
        for(int i=0; i<m; i++){
          if(row[i]){
              r ++ ;
          }
        }
        for(int i=0; i<n; i++){
            if(column[i]){
                c ++ ;
            }
        }
        return r*n + c*m - 2*r*c ;

    }
}


8 - build array with stack operation
Title Link: Title Link stamp here!!!

Idea: just simulate the whole process. From 1 to n, when the value is equal to target every time, add Push to the list set. If the value is less than target every time, Push and Pop of target-i are required.

class Solution {
    public List<String> buildArray(int[] target, int n) {
       List<String> ans = new ArrayList<>() ;
       int j = 0 ;
       for(int i=1; i<=n; i++){
           if(j > target.length-1 || target[j]<i){
               break ;
           }
           if(target[j]==i){
               ans.add("Push") ;
               j ++ ;
           }else if(target[j]>i){
               for(int k=0; k<target[j]-i; k++){
                   ans.add("Push") ;
                   ans.add("Pop") ;
               }
               i += (target[j]-i-1) ;
           }
           
       } 
       return ans ;
    }
}


9 - wine change problem
Title Link: Title Link stamp here!!!

Idea: just simulate the wine changing process. As long as the current number of wine bottles is greater than the exchange number, cycle, and calculate the number of drinks res in each cycle. At the same time, update the number of wine bottles numbolts.

class Solution {
    public int numWaterBottles(int numBottles, int numExchange) {
        int res = 0 ;
        int temp = numBottles ;
        while(numBottles>=numExchange){
            int n = numBottles / numExchange ;
            res += (numBottles / numExchange) ;
            numBottles = (numBottles%numExchange) + n ;
        }
        return res+temp ;
    }
}


10 - find the players of the array game
Title Link: Title Link stamp here!!!

Idea 1: use list set to simulate the whole process. Although it can also AC, it is inefficient.

class Solution {
    public int getWinner(int[] arr, int k) {
        int [] res = new int [1000000] ;
        int max = 0 ;
        List<Integer> list = new ArrayList<>() ;
        for(int i=0; i<arr.length; i++){
            list.add(arr[i]) ;
            max = Math.max(max,arr[i]) ;
        }
        while(true){
            if(list.get(0)==max || list.get(1)==max){
                return max ;
            }
            if(list.get(0)>list.get(1)){
                int t = list.get(0) ;
                res[t] ++ ;
                int temp = list.get(1) ;
                list.remove(1) ;
                list.add(temp) ;
                if( res[t]==k){
                    return t ;
                }
            }else{
                int t = list.get(1) ;
                res[t] ++ ;
                int temp = list.get(0) ;
                list.remove(0) ;
                list.add(temp) ;
                if(res[t]==k){
                    return t ;
                }
            }
        }  
    }
}


11 - create the target array in the given order
Title Link: Title Link stamp here!!!

Idea: first update the index array, and then update the result array ans.

class Solution {
    public int[] createTargetArray(int[] nums, int[] index) {
       int len = nums.length ;
       int [] ans = new int [len] ;
       for(int i=0; i<len; i++){
           for(int j=0; j<i; j++){
               index[j] += (index[j]>=index[i]) ? 1 : 0 ;
           }
       }
       for(int i=0; i<ans.length; i++){
           ans[index[i]] = nums[i] ;
       }
       return ans ;
    }
}


12 - Queen who can attack the king
Title Link: Title Link stamp here!!!

Idea: the simulation process is to use the king to attack the queen and attack in eight directions. As long as you can attack one at a time, you will end the attack in the current direction. If you can't attack beyond the boundary, you will end the attack in the current direction.

class Solution {
     int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
     int dy[] = {-1,  0,  1, 1, 1, 0,-1,-1};
    public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
        List<List<Integer>> list = new ArrayList<>() ;
        int [][] grid = new int [8][8] ; 
        for(int i=0; i<queens.length; i++){
            grid[queens[i][0]][queens[i][1]] = 1 ;
        }
        for(int i=0; i<8; i++){
            int tx = king[0] + dx[i] ;
            int ty = king[1] + dy[i] ;
            while(tx>=0 && ty>=0 && tx<8 && ty<8){
                if(grid[tx][ty]==1){
                List<Integer> temp = new ArrayList<>() ;
                temp.add(tx) ;
                temp.add(ty) ;
                list.add(temp) ;
                 break ;
                }
                tx += dx[i] ;
                ty += dy[i] ;
            }
        }
        return list ;
    }
}

Topics: Algorithm leetcode Simulation