今日头条 8.12笔试

第五题:最多能看多少个直播节目

import java.util.*;
public class Main {
    public static void main(String[] args) {

        class qujian implements Comparable{
            public int l;
            public int r;
            @Override
            public int compareTo(qujian o) {
                return this.r - o.r;
            }
        }

        int left,right;
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        List list = new LinkedList<>();
        for(int i = 0; i < n; i++){
            qujian temp = new qujian();
            temp.l = scanner.nextInt();
            temp.r = scanner.nextInt();
            if(temp.l > temp.r){
               temp.r += m;
            }
            list.add(temp);
        }

        int result = 0;
        Collections.sort(list);
        for(int i = 0; i < n; i++){
            int end = list.get(i).r;
            int tot = 1;
            for(int j = 1; j < n; j++){
                if(i + j < n && list.get(i+j).l >= end){
                    tot++;
                    end = list.get(i+j).r;
                }
                if(i + j >= n && list.get(i+j-n).l + m >= end){
                    tot++;
                    end = list.get(i+j-n).r + m;
                }
            }
            result = Math.max(result,tot);
        }
        System.out.println(result);
    }
}

你可能感兴趣的:(今日头条 8.12笔试)