[LeetCode] 1094. Car Pooling 拼车


You are driving a vehicle that has capacity empty seats initially available for passengers.  The vehicle only drives east (ie. it cannot turn around and drive west.)

Given a list of tripstrip[i] = [num_passengers, start_location, end_location] contains information about the i-th trip: the number of passengers that must be picked up, and the locations to pick them up and drop them off.  The locations are given as the number of kilometers due east from your vehicle's initial location.

Return true if and only if it is possible to pick up and drop off all passengers for all the given trips.

Example 1:

Input: trips = [[2,1,5],[3,3,7]], capacity = 4
Output: false

Example 2:

Input: trips = [[2,1,5],[3,3,7]], capacity = 5
Output: true

Example 3:

Input: trips = [[2,1,5],[3,5,7]], capacity = 3
Output: true

Example 4:

Input: trips = [[3,2,7],[3,7,9],[8,3,9]], capacity = 11
Output: true

Constraints:

  1. trips.length <= 1000
  2. trips[i].length == 3
  3. 1 <= trips[i][0] <= 100
  4. 0 <= trips[i][1] < trips[i][2] <= 1000
  5. 1 <= capacity <= 100000

这道题说的是拼车的那些事儿,给了一个数组,里面是很多三元对儿,分别包含乘客个数,上车时间和下车时间,还给了一个变量 capacity,说是任何时候的乘客总数不超过 capacity 的话,返回 true,否则就返回 false。这道题其实跟之前的 Meeting Rooms II 是一样,只不过那道题是求需要的房间的总个数,而这里是限定了乘客的总数,问是否会超载。使用的解题思想都是一样的,主要是需要将上车时间点和下车时间点拆分开,然后按时间顺序排列在同一条时间轴上,上车的时候就加上这些人数,下车的时候就减去这些人数。若某个时间点上的总人数超过了限定值,就直接返回 false 就行了,这里博主没有用 TreeMap,而是直接都放到一个数组中,然后对该数组按时间点进行排序,再遍历排序后的数组,进行累加元素之和即可,参见代码如下:


解法一:

class Solution {
public:
    bool carPooling(vector<vector<int>>& trips, int capacity) {
        int cur = 0;
        vector<vector<int>> data;
        for (auto trip : trips) {
            data.push_back({trip[1], trip[0]});
            data.push_back({trip[2], -trip[0]});
        }
        sort(data.begin(), data.end());
        for (auto &a : data) {
            cur += a[1];
            if (cur > capacity) return false;
        }
        return true;
    }
};

接下来看一种更加高效的解法,并不用进行排序,那个太耗时了。题目限定了时间点不会超过 1000,所以这里就建立一个大小为 1001 的 cnt 数组,然后遍历 trips 数组,将对应的上车时间点加上乘客人数,下车时间点减去乘客人数,这样的话就相当于排序完成了,有点计数排序的感觉。之后再遍历这个 cnt 数组,累加当前的值,只要超过 capacity 了,就返回 false,否则最终返回 true 即可,参见代码如下:


解法二:

class Solution {
public:
    bool carPooling(vector<vector<int>>& trips, int capacity) {
        int cur = 0;
        vector<int> cnt(1001);
        for (auto &trip : trips) {
            cnt[trip[1]] += trip[0];
            cnt[trip[2]] -= trip[0];
        }
        for (int i = 1; i <= 1000; ++i) {
            cur += cnt[i];
            if (cur > capacity) return false;
        }
        return true;
    }
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1094


类似题目:

Meeting Rooms II


参考资料:

https://leetcode.com/problems/car-pooling/

https://leetcode.com/problems/car-pooling/discuss/317610/JavaC%2B%2BPython-Meeting-Rooms-III

https://leetcode.com/problems/car-pooling/discuss/317611/C%2B%2BJava-O(n)-Thousand-and-One-Stops


LeetCode All in One 题目讲解汇总(持续更新中...)

posted @ 2021-04-02 13:04  Grandyang  阅读(1126)  评论(1编辑  收藏  举报
Fork me on GitHub