1094. Car Pooling

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

给一系列车次,包含上车人数,上车站点和下车站点,站点可看作在一条直线上且不会回头,问给定容量的Bus能否把所有人都送到站。核心和meeting rooms III是一致的:带有高度的可重叠interval。对每个点排好序后从一个个点扫描并+/-上高度,当高度大于容量则说明不能。

/*
 * @lc app=leetcode id=1094 lang=cpp
 *
 * [1094] Car Pooling
 */

// @lc code=start
class Solution {
public:
    bool carPooling(vector<vector<int>>& trips, int capacity) {
        map<int, int> points;
        for (const auto &t : trips) {
            points[t[1]] += t[0];
            points[t[2]] -= t[0];
        } 
        int s = 0;
        for (auto &p : points) {
            s += p.second;
            if (s > capacity) return false;
        } 
        return true;
    }
};
// @lc code=end

Last updated