今日头条校招笔试

4
20 23 35 40
满足:
1.  a<=b<=c
2. b-a<=10
3. c-b<=10
求最少还需要多少个数才能满足。
每套试卷3道题,且不能重复。
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    private static void pop(ArrayDeque deque, int n) {
        for (int i = 0; i < n; i++) {
            deque.pollLast();
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while (cin.hasNext()) {
            int n = cin.nextInt();
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = cin.nextInt();
            }
            Arrays.sort(a);
            ArrayDeque deque = new ArrayDeque<>();
            int ans = 0;
            for (int i = 0; i < n; i++) {
                if (!deque.isEmpty()) {
                    while (!deque.isEmpty() && a[i] - deque.peek() > 10) {
                        deque.offerFirst(deque.peek() + 10);
                        ans++;
                        while (deque.size() >= 3) {
                            pop(deque, 3);
                        }
                    }
                }
                deque.offerFirst(a[i]);
                while (deque.size() >= 3) {
                    pop(deque, 3);
                }
            }
            System.out.println(ans + (deque.size() == 0 ? 0 : 3 - deque.size()));
        }
    }
}

你可能感兴趣的:(笔试面试总结)