[a daily question with a little insight] simulation question - three in one (number of matches, diagonal experience, life game)

Posted by mindspin311 on Tue, 25 Jan 2022 13:42:41 +0100

⭐ New pit in winter vacation -- daily question notes of code Fox

1688. Number of matches in the competition

Give you an integer n to represent the number of teams in the game. The competition follows a unique competition system:

  • If the current number of teams is even, each team will be paired with another team. A total of n / 2 games were played, and n / 2 teams were produced to enter the next round.
  • If the current number of teams is odd, it will be randomly emptied and promoted to one team, and the other teams will be paired. A total of (n - 1) / 2 games will be played, and (n - 1) / 2 + 1 teams will be produced to enter the next round.

Returns the number of matches made in the game until the winning team is determined.

class Solution {
    public int numberOfMatches(int n) {
        if(n==1){
            return 0;
        }
        int ans=0;
        while(n!=1){
            ans=ans+n/2;
            n=(n+1)/2;
        }
        return ans;
    }
}

//A total of n-1 teams will be eliminated, requiring n-1 games
class Solution {
    public int numberOfMatches(int n) {
        return n - 1;
    }
}

498. Diagonal traversal Mid

Title Description:

Give you a matrix mat with the size of m x n. please use an array to return all the elements in the matrix in the order of diagonal traversal.

Problem solving ideas:

Simulate the traversal path and pay attention to cross-border fallback and end point judgment (see code for details)

Code implementation:

class Solution {
    public int[] findDiagonalOrder(int[][] mat) {
        int m=mat.length;
        int n=mat[0].length;
        
        int[] ans=new int[m*n];
        //Fill in position
        int cur=0;
        //Record the position of the array
        int x=0;
        int y=0;
        
        //Repeat operation
        while(true){
            //Go up obliquely until x or Y crosses the boundary. Since there is only x -- (row rising), y + + (column moving to the right)
            //Fill in the array
            while(x>=0&&y<n){
                ans[cur++]=mat[x][y];
                x--;
                y++;
            }
            //x fallback
            x++;
            //x==m indicates the last line, that is, the completion of the pass
            if(x==m){
                return ans;
            }
            //If y goes back out of bounds, and x drops one line again
            //If y crosses the boundary, you need to fall back x and y, and then drop the line one line
            //If only x is out of bounds, X and y will fall back, and then move the column to the right one column (that is, it is equivalent to not falling back y)
            if(y>=n){
                x++;
                y--;
            }
            
			//The oblique downward operation is roughly the same
            while(x<m&&y>=0){
                ans[cur++]=mat[x][y];
                x++;
                y--;
            }
            y++;
            if(y==n){
                return ans;
            }
            if(x>=m){
                x--;
                y++;
            }
        }
    }
}

289. Life game - Hard

Title Description:

according to Baidu Encyclopedia , life game, or life for short, is a cellular automata invented by British mathematician John Horton Conway in 1970.

Given a containing m × n grid panels, each grid can be regarded as a cell. Each cell has an initial state: 1 is a living cell, or 0 is a dead cell. Each cell and its eight adjacent positions (horizontal, vertical, diagonal) follow the following four survival laws:

  1. If the number of living cells at eight locations around the living cells is less than two, the living cells at that location die;
  2. If there are two or three living cells at eight locations around the living cells, the living cells at that location still survive;
  3. If there are more than three living cells at eight locations around the living cells, the living cells at that location die;
  4. If there are exactly three living cells around the dead cells, the dead cells at this position will be revived;

The next state is formed by applying the above rules to each cell in the current state at the same time, in which the birth and death of cells occur at the same time. Give you the current status of m x n grid panel board and return to the next status.

Problem solving ideas:

Basic idea: count the number of 1 around. According to the meaning of the question, 3 1 must be updated to 1; 2 1, do not change the state; Others must be 0;

Problem solution 1: use a new array to save the update status (the problem stipulates to modify in place, so it can't be used)

Problem solution 2: when updating, if the status (0 - > 1, 1 - > 0) is modified, it is recorded but not involved in statistics. Go through it again and write the record

class Solution {
    public void gameOfLife(int[][] board) {
        int[] dx=new int[]{-1,0,1,-1,1,-1,0,1};
        int[] dy=new int[]{-1,-1,-1,0,0,1,1,1};
        
        int m=board.length;
        int n=board[0].length;

        for(int x=0;x<m;x++){
            for(int y=0;y<n;y++){
                int count=0;
                int index=8;
                while(index-->0){
                    if(x+dx[index]<0||x+dx[index]>=m||y+dy[index]<0||y+dy[index]>=n){
                        ;
                    }
                    else if(board[x+dx[index]][y+dy[index]]==1||board[x+dx[index]][y+dy[index]]==-1){
                        count++;
                    }
                }
                if(count==3){
                    if(board[x][y]!=1){
                        board[x][y]=-2;//-2 means from 0 to 1
                    }
                }
                else if(count<2||count>3){
                    if(board[x][y]!=0)
                        board[x][y]=-1;//-1 means to change from 1 to 0
                }
            }
        }
        //History write
        for(int x=0;x<m;x++){
            for(int y=0;y<n;y++){
                if(board[x][y]==-2){
                    board[x][y]=1;
                }
                if(board[x][y]==-1){
                    board[x][y]=0;
                }
            }
        }
    }
}

ending

Title Source: LeetCode link: https://leetcode-cn.com/problems

⭐ Pay attention to the author, take you to brush the questions, and learn the most commonly used algorithm skills from simple algorithm questions (one question per day in winter vacation)
⭐ Pay attention to the author's problem brushing - simple to advanced, which makes you unknowingly become a ruthless problem brushing machine. Please send a private letter if you have any questions

Topics: Algorithm leetcode