Test Development Preparation for Autumn Recruitment Interview 18-Greedy + Simulation

After working hard for so many years, looking back, it is almost all long setbacks and sufferings. For most people's life, smooth sailing is only occasional, and frustration, unbearable, anxiety and confusion are the main theme. We take the stage we did not choose, play the script we did not choose. Keep going!

Table of contents

1. Dividing candies

2. Moderator scheduling (2)

3. Rotate the array

4. Spiral Matrix

5. Clockwise matrix rotation


1. Dividing candies

Topic Link: Dividing Candy Problem_Niuke Question Master_Niuke.com

Idea: The main thing is to meet two conditions, initialize the array element to 1, and satisfy the first one. For the second condition, there are two rounds of cycles from right to right and right to left to ensure that the adjacent large points have more candies. That is, there are more points than the left and right points.

Java version:

import java.util.*;


public class Solution {
    /**
     * pick candy
     * @param arr int整型一维数组 the array
     * @return int整型
     */
    public int candy (int[] arr) {
        // write code here
        if(arr.length == 1){
            return 1 ;
        }
        int [] candy = new int [arr.length]  ;
        Arrays.fill(candy, 1) ;
        for(int i=1; i<arr.length; i++){
            if(arr[i-1] < arr[i]){
                candy[i] = candy[i-1] + 1 ;
            }
        }
        for(int i=arr.length-1; i>0; i--){
            if(arr[i-1] > arr[i]){
                candy[i-1] = Math.max(candy[i-1],candy[i] + 1) ;
            }
        }
        int ans = 0 ;
        for(int s : candy){
            ans += s ;
        }
        return ans ;
    }
}

2. Moderator scheduling (2)

Topic Link: Moderator Scheduling (2)_Niuke Topic_Niuke.com
Idea: Define two arrays, record the start time and end time, sort first, then sort, if the next start time is greater than the current end time, it is not needed Add a host, otherwise you need to add a host.

Java version:

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 计算成功举办活动需要多少名主持人
     * @param n int整型 有n个活动
     * @param startEnd int整型二维数组 startEnd[i][0]用于表示第i个活动的开始时间,startEnd[i][1]表示第i个活动的结束时间
     * @return int整型
     */
    public int minmumNumberOfHost (int n, int[][] startEnd) {
        // write code here
        int [] s = new int [n] ;
        int [] e = new int [n] ;
        for(int i=0; i<n; i++){
            s[i] = startEnd[i][0] ;
            e[i] = startEnd[i][1] ;
        }
        Arrays.sort(s) ;
        Arrays.sort(e) ; 
        int ans = 0 ;
        int j = 0 ;
        for(int i=0; i<n; i++){
            if(s[i] >= e[j]){
                j ++ ;
            }else{
                ans ++ ;
            }
        }
        return ans ;

    }
}

3. Rotate the array

Topic Link: Rotate Array_Niuke Topic_Niuke.com

Idea: Three flips can simulate the rotation of the array. Note that m may be larger than n, so take the remainder.

Java version:

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 旋转数组
     * @param n int整型 数组长度
     * @param m int整型 右移距离
     * @param a int整型一维数组 给定数组
     * @return int整型一维数组
     */
    public int[] solve (int n, int m, int[] a) {
        // write code here
        m = m % n ;
        reverse(a,0,n-1) ;
        reverse(a,0,m-1) ;
        reverse(a,m,n-1) ;
        return a ;
    }
    public void reverse(int [] a, int left, int right){
        while(left<right){
            int tmp = a[left] ;
            a[left] = a[right] ;
            a[right] = tmp ;
            left ++ ;
            right -- ;
        }
    }
}

4. Spiral Matrix

Topic Link: Spiral Matrix_Niuke Question Master_Niuke.com

Idea: Define the four corners of top, bottom, left, and right, and then simulate walking the four sides, and then store them in the collection in turn.

Java version:

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param matrix int整型二维数组 
     * @return int整型ArrayList
     */
    public ArrayList<Integer> spiralOrder (int[][] matrix) {
        // write code here
        ArrayList<Integer> list = new ArrayList<>() ;
        if(matrix.length == 0){
            return list ;
        }
        int top = 0 ;
        int bottom = matrix.length - 1;
        int left = 0 ;
        int right = matrix[0].length - 1;

        while(true){
            for(int i=left; i<=right; i++){
                list.add(matrix[top][i]) ;
            }
            top ++ ;
            if(top>bottom){
                break ;
            }
            for(int i=top; i<=bottom; i++){
                list.add(matrix[i][right]) ;
            }
            right -- ;
            if(right < left){
                break ;
            }
            for(int i=right; i>=left; i--){
                list.add(matrix[bottom][i]) ;
            }
            bottom -- ;
            if(bottom < top){
                break ;
            }
            for(int i=bottom; i>=top; i--){
                list.add(matrix[i][left]) ;
            }
            left ++ ;
            if(left > right){
                break ;
            }
        }
        return list ;
    }
}

5. Clockwise matrix rotation

Topic Link: Clockwise Rotation Matrix_Niuke Topic_Niuke.com

Idea: You can open up a new array and find the subscript mapping relationship before and after rotation. It is also possible to directly exchange diagonal elements on the original array, and then exchange up and down or left and right elements to simulate flipping.

Java version:

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param mat int整型二维数组 
     * @param n int整型 
     * @return int整型二维数组
     */
    public int[][] rotateMatrix (int[][] mat, int n) {
        // write code here
        int [][] ans = new int [n][n] ;
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                ans[j][n-i-1] = mat[i][j] ;
            }
        }
        return ans ;
    }
}

Guess you like

Origin blog.csdn.net/nuist_NJUPT/article/details/131217058
Recommended