2022 8.13 Meituan Backend Written Exam

Please note in advance that this article is a re-creation of Niuke Dao’s article: Meituan 8.13 back-end direction written test questions are all AC_Bijingjingjing_Niuke.com

1: Magic Takeaway (Simulation)

* The fried chicken restaurant has a delivery boy who can teleport magic. 
 * 
 * When the delivery person delivers the order, he can spend time t to deliver the order normally (only one order can be delivered at a time, not multiple orders at the same time), or he can use magic to deliver the order instantly through the air without consuming time. 
 * 
 * Now the fried chicken shop has received a number of fried chicken orders at time 0, and each order has its deadline for delivery. The delivery person needs to ensure that the delivery time is less than or equal to this deadline. 
 * 
 * Now ask the delivery boy at least how many times he needs to use magic to ensure that the delivery does not time out. 
 * 
 * 
 * 
 * Input description 
 * The first line contains two positive integers n and t separated by spaces, indicating that n orders are currently received and the time t takes for the delivery staff to deliver normally without using magic. 
 * 
 * The second line contains n positive integers, each positive integer represents the deadline for delivery of an order. 
 * 
 * 1 <= n <= 1e5, 1 <= t <= 100, the delivery time of the order is between [1, 1e7] * * Output 
 description 
 * 
 One non-negative integer in one line, indicating the minimum number of times the delivery person needs to use Magic times. 
 * Sample input 
 * 6 5 
 * 5 6 7 8 9 10 
 * Sample output 
 * 4
public class Main1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n=scanner.nextInt();
        int t=scanner.nextInt();
        int[] times = new int[n];
        for (int i=0; i<times.length; i++){
            times[i] = scanner.nextInt();
        }
        Arrays.sort(times);
        int num=0;
        int time=0;
        for (int i=0; i<times.length; i++){
            //送完前一个的时间+t小于等于下一个的截止时间,就不用魔法
            if(time+t<=times[i]){
                time+=t;
            }else{
                num++;
            }
        }
        System.out.print(num);
    }
}

2: Clean the room (simulation)

You bought a sweeping robot and you want to know whether it can clean the room. 

* To simplify the problem, we might as well assume that the room is divided into n*m squares. Cleaning is defined as all n*m squares that have been cleaned at least once. 
* <p> 
* You gave several instructions to the sweeping robot. Each command is one of up, down, left, and right movements. The robot will clean the squares on the path it passes. 
* <p> 
* Initially, it is assumed that the robot is in the square of the first row and first column. This square will be cleaned directly by the robot initially. 

* Now ask you whether the robot can clean the room. If it can, it will output Yes, if it cannot, it will output No. 
* <p> 
* In the case of Yes, you are also asked to continue outputting to which command ends, and the room will be cleaned. 
* <p> 
* In the case of No, you are also asked to output how many plots have not been cleaned. 
* <p> 
* Ensure that the robot will not cross the room boundary during cleaning. In other words, the robot always remains in the n*m ​​grid. 

* Input description 
* Three positive integers n, m, in the first line k, separated by spaces, indicates the room size n*m, followed by instructions of length k. 
* <p> 
* The second line is a string of length k. There are only the following four characters in the string (note: all are uppercase) 
* <p> 
* W: Indicates that the robot will move up in the next step 
* <p> 
* A: Indicates that the robot will move to the left in the next step 
* <p> 
* S: Indicates that the robot will move downward next 
* <p>
* D: Indicates that the robot will move to the right in the next step 
* <p> 
* Guaranteed 2 <= n, m <= 100, instruction length <= 100000 
* <p> 
* Output description 
* The first line represents a string of Yes or No Can it be cleaned? 
* <p> 
* In the case of Yes, the second line outputs a positive integer, indicating that it has been cleaned after the number of instructions. 
* <p> 
* Note that the instruction number starts from 1 instead of 0. 
* <p> 
* For the No case, the second line outputs a positive integer, indicating how many plots are left to be cleaned. 

* Sample input 
* 2 2 5 
* SDWAS 
* Sample output 
* Yes 
* 3
public class Main2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int k = scanner.nextInt();

        String orders = scanner.next();
        if (orders.length() != 5) {
            return;
        }
        int[][] room = new int[n][m];
        room[0][0] = 1;
        int count = 1, x = 0, y = 0;
        for (int i = 0; i < orders.length(); i++) {
            char ch = orders.charAt(i);
            if (ch == 'W') {
                y--;
            } else if (ch == 'S') {
                y++;
            } else if (ch == 'A') {
                x--;
            } else if (ch == 'D') {
                x++;
            }
            //存在这个点
            if (room[y][x] == 0) {
                room[y][x] = 1;
                count++;
            }
            if (count == n * m) {
                System.out.println("Yes");
                System.out.print(i + 1);
                return;
            }

        }
        System.out.println("No");
        System.out.print(n * m - count);
    }
}

3: Poker game (two-way queue, reverse simulation)

* Alice and Bob are playing a game. There are n cards, with points ranging from 1 to n. After shuffling, n cards are stacked from top to bottom to form a pile. Each time Alice puts the top card of the current deck to the bottom of the deck, and then Bob puts the top card of the current deck to the bottom of the deck. (Specifically, when there is only one card in the deck, it is equivalent to doing nothing.) Then, they will turn over the top card of the current deck and record its value. When all the cards were revealed, they also recorded n points. Now they want to restore the original cards (the points of each card from the top of the deck to the bottom of the deck) based on the sequence they wrote down. 
* <p> 
* <p> 
* <p> 
* Input description 
* The first line is a positive integer n, indicating that there are n cards. 
* <p> 
* The next line is n positive integers separated by spaces. The i-th number a_i represents the point of the i-th card that was turned over. 
* <p> 
* 1<=n<=100000 
* <p> 
* Output description 
* A line of n positive integers separated by spaces. The i-th number represents the number from the top to the bottom of the initial deck. The points of i cards. 
* <p> 
* <p> 
* Sample input 
* 4 
* 1 2 3 4 
* Sample output 
* 4 2 1 3

public class Main3 {
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n=scanner.nextInt();
        int[] cards = new int[n];
        for(int i=0; i<n; i++)  {
            cards[i] = scanner.nextInt();
        }
        int[] origin=new int[n];
        Deque<Integer> deque = new LinkedList<Integer>();
        for(int i=0; i<n; i++) {
            deque.offerLast(n-i-1);
        }
        for(int i=0; i<n; i++) {
            deque.offerFirst(deque.pollLast());
            deque.offerFirst(deque.pollLast());
            //反向操作,现在索引是正向的
            origin[deque.pollLast()]=cards[i];
        }
        for(int i=0; i<n; i++) {
            System.out.print(origin[i]);
            System.out.print(" ");
        }

    }
}

4: Triple deformation problem (HashMap)

Given a sequence a[1], a[2], …, a[n] of length n, how many triples (i, j, k) are there that satisfy i<j<k and a[i]- a[j]=2a[j]-a[k]? Output the number of triples that meet the requirements. 
* 
* 
* 
* Input description 
* The first line is an integer n, indicating that the sequence length is n. 
* 
* The next line contains n integers separated by spaces, a[i] represents the i-th number of the sequence. 
* 
* 1<=n<=4000, 0<=a[i]<=1000000 
* 
* Output description 
* One integer per line, indicating the number of triples that meet the requirements. 
* 
* 
* Sample input 
* 4 
* 4 2 2 2 
* Sample output 
* 3
public class Main4 {
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n=scanner.nextInt();
        int[] nums = new int[n];
        for(int i=0; i<n; i++) {
            nums[i] = scanner.nextInt();
        }
        long count=0;

        Map<Integer, Integer> mp = new HashMap<>();
        mp.put(nums[n - 1], 1);
        for (int i = n - 2; i >= 1; i--) {
            for (int j = i - 1; j >= 0; j--) {
                count += mp.getOrDefault(3 * nums[i] - nums[j], 0);
            }
            mp.put(nums[i], mp.getOrDefault(nums[i], 0) + 1);
        }

        /*超时了,82%
        int num=0;

        for(int i=0;i<arr.length;i++) {
            for(int j=i+1; j<arr.length; j++) {
                for(int k=j+1; k<arr.length; k++) {
                    if(arr[i]==3*arr[j]-arr[k]) {
                        num++;
                    }
                }
            }
        }
        System.out.print(num);*/
        System.out.print(count);
    }
}


5: Maximum branch value of the tree (level-order traversal)

Given a binary tree with n nodes, the nodes are numbered from 1 to n. 
* 
* Among them, the left son of node k is node 2*k (when 2*k is greater than n, there is no left son) * * The right son 
of 
node k is node 2*k+1 (when 2*k+1 is greater than When n, there is no right son) 
* 
* The root node of this binary tree is node 1. 
* 
* 
* 
* For each node, there is some money on the node. 
* 
* Now you can start from the root node 1, choose the left son or the right son each time and go down until you reach the leaf node and stop, and get the money on these nodes you walked through. 
* 
* 
* 
* Your task is to find the maximum amount of money you can obtain. 
* 
* 
* 
* Input description 
* The first line is a positive integer n, indicating that there are a total of n nodes on the binary tree. 
* 
* The second line has n positive integers, and the i-th positive integer represents the amount of money on node i. 
* 
* 1 <= n <= 100000 
* 
* Guaranteed for all data: the amount of money on a single node is between [1, 1000] 
* 
* Output description 
* One positive integer per line, representing the maximum total amount of money you can obtain Quantity 
* 
* 
* Sample input
* 3
* 5 7 8 
* Sample output 
* 13
public class Main5 {
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n=scanner.nextInt();
        int[] array = new int[n+1];
        for (int i=1; i<=n; i++) {
            array[i] = scanner.nextInt();
        }
        int res=0;
        Queue<Integer> deque = new ArrayDeque<Integer>();
        deque.offer(1);
        while(!deque.isEmpty()) {
            int tmp=deque.poll();
            res=Math.max(res,array[tmp]);
            if(2*tmp<=n){
                array[tmp*2]+=array[tmp];
                deque.offer(2*tmp);
            }
            if(2*tmp+1<=n){
                array[tmp*2+1]+=array[tmp];
                deque.offer(2*tmp+1);
            }
        }
        System.out.print(res);
    }
}

Guess you like

Origin blog.csdn.net/weixin_45889893/article/details/126324993