Meituan 3.25 Written Test Record - Question 1

first question:

Title description:

Xiaomei is a train fan. Recently, she was observing the incoming and outgoing trains at the train station near her home, and found that the order of the trains entering and exiting was not consistent. After investigation by Xiaomei, it turns out that there is a structure similar to a stack in the train station, as shown in the figure below: For example, train No. 1 may have entered the rest area s in the train station, and train No. 2 has entered the rest area before leaving. . So in this case, train No. 1 needs to wait for train No. 2 to back out before going out (obviously blocked by train No. 2 entering from behind, and this rest area s has only one entrance and exit). Out of curiosity, Xiaomei counted the incoming and outgoing trains in recent days, and the rest area s was empty when the statistics started and ended. Due to negligence in the middle, Xiaomei feels that she seems to have made a few mistakes in the sequence of entering and exiting, and I would like to ask you to verify it for her. It is worth noting that although Xiaomei may have made a mistake in the order, she does not repeat the records of the trains. To formally describe the rest area s, we regard it as a space with infinite capacity, assuming that two trains i and j are in the rest area s at the same time, and the entry time Tin satisfies Tin(i)<Tin(j), then The departure time Tout must satisfy Tout(i)>Tout(j), that is, first in last out.

enter description

An integer T in the first line represents the number of data sets.

For each set of tests:
the first line contains an integer n, representing the number of trains observed.
The second line contains n integers x1, x2,...,xn, indicating the order in which the train entered the rest area s recorded by Xiaomei.
The third line contains n integers y1, y2,..., yn, which represent the sequence of the train leaving the rest area s recorded by Xiaomei.
1≤T≤10, 1≤n≤50000, 1≤xi, yi≤n, and {xn} and {yn} are an arrangement of {1,2,3,…,n}, that is, 1~n There are n numbers in which there are no repetitions and no omissions appear exactly once.

output description

Output one line for each set of data: if the entry and exit sequence recorded by Xiaomei cannot be satisfied, output No, otherwise output Yes.

sample input

3
3
1 2 3
1 2 3
3
1 2 3
3 2 1
3
1 2 3
3 1 2

sample output

Yes
Yes
No

answer

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

/**
 * 第一题
 */
public class Test01 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();//表示数据组数int
        String[] res = new String[T];
        for(int i = 0; i < T; i++){
    
    
            int n = sc.nextInt();//表示观察到的火车数量
            int[] pushed = new int[n];//火车入栈数组
            int[] popped = new int[n];//火车出栈数组
            for(int j = 0; j < n; j++){
    
    
                pushed[j] = sc.nextInt();
            }
            for(int k = 0; k < n; k++){
    
    
                popped[k] = sc.nextInt();
            }
            res[i] = judge(pushed, popped);
        }
        for(String s: res){
    
    
            System.out.println(s);
        }
    }

    public static String judge(int[] pushed,int[] popped){
    
    
        Deque<Integer> stack = new ArrayDeque<>();
        int n = pushed.length;
        for(int i = 0,j = 0;i < n;i++){
    
    
            stack.push(pushed[i]);
            while(!stack.isEmpty() && stack.peek() == popped[j]){
    
    
                stack.pop();
                j++;
            }
        }
        return stack.isEmpty() ? "Yes" : "No";
    }
}

train of thought

The judge() method is to judge whether the sequence of trains recorded by Xiaomei satisfies the sequence of popping out of the stack, which is consistent with the title of Jianzhi Offer 31. Stack push and pop sequence.

Note:

1. So why don't we choose Stack directly, but use Deque?

There are two main reasons:

  1. In terms of performance, Deque should be used instead of Stack.
    Both Stack and Vector are thread-safe. In fact, thread safety is not required in most cases, so there is no need to use Stack. After all, to ensure thread safety requires locking, there is additional system overhead.
  2. The inheritance of Stack from Vector is a historical issue. The JDK official has suggested that the implementation class of Deque should be used instead of Stack.
    A side effect of Stack inheriting from Vector is that it exposes the set/get method and can access random positions, which is contrary to the original intention that Stack can only increase or decrease from the tail.

In addition, Deque maintains the semantics of "last in, first out" when converted to ArrayList or stream, while Stack does not have this semantics because it inherits from Vector.

Stack<Integer> stack = new Stack<>();
Deque<Integer> deque = new ArrayDeque<>();

stack.push(1);
stack.push(2);
deque.push(1);
deque.push(2);

System.out.println(new ArrayList<>(stack)); // [1,2]
List<Integer> list1 = stack.stream().collect(Collectors.toList());//[1,2]

// deque转成ArrayList或stream时保留了“后进先出”的语义
System.out.println(new ArrayList<>(deque)); // [2,1]
List<Integer> list2 = deque.stream().collect(Collectors.toList());//[2,1]

2. So should we use ArrayDeque or LinkedList to create new objects?

The bottom layer of ArrayDeque and LinkedList, one uses array storage and the other uses linked list storage;

  1. Array storage, when the capacity is not enough, it needs to expand and copy the array, usually the capacity will not be filled, and there will be space waste;

  2. For linked list storage, each push requires a new Node node, and there are prev and next members in the node node, which will also occupy additional space.

    Summary:
    ArrayDeque will be slightly better, but the difference is usually negligible. After performance comparison, it is more inclined to use ArrayDeque to express the stack function in Java.

Reference article: https://blog.csdn.net/qq_37509948/article/details/123022630

Guess you like

Origin blog.csdn.net/qq_44678607/article/details/129779957