【扫描线】LeetCode 1094. 拼车

题目链接

1094. 拼车

思路

【扫描线】LeetCode 253. 会议室 II 思路一致

代码

class Solution {
    public boolean carPooling(int[][] trips, int capacity) {
        int[] temp = new int[1000 + 3];
        for(int[] trip : trips){
            temp[trip[1]] += trip[0];
            temp[trip[2]] -= trip[0];
        }

        int sum = 0;
        int maxPopulation = 0;
        for(int population : temp){
            sum += population;
            maxPopulation = Math.max(maxPopulation, sum);
        }

        return maxPopulation <= capacity;
    }
}
posted @ 2023-02-28 16:41  Frodo1124  阅读(25)  评论(0编辑  收藏  举报