Skip to content

Latest commit

 

History

History
197 lines (156 loc) · 5.08 KB

File metadata and controls

197 lines (156 loc) · 5.08 KB

English Version

题目描述

假设你是一位顺风车司机,车上最初有 capacity 个空座位可以用来载客。由于道路的限制,车 只能 向一个方向行驶(也就是说,不允许掉头或改变方向,你可以将其想象为一个向量)。

这儿有一份乘客行程计划表 trips[][],其中 trips[i] = [num_passengers, start_location, end_location] 包含了第 i 组乘客的行程信息:

  • 必须接送的乘客数量;
  • 乘客的上车地点;
  • 以及乘客的下车地点。

这些给出的地点位置是从你的 初始 出发位置向前行驶到这些地点所需的距离(它们一定在你的行驶方向上)。

请你根据给出的行程计划表和车子的座位数,来判断你的车是否可以顺利完成接送所有乘客的任务(当且仅当你可以在所有给定的行程中接送所有乘客时,返回 true,否则请返回 false)。

 

示例 1:

输入:trips = [[2,1,5],[3,3,7]], capacity = 4
输出:false

示例 2:

输入:trips = [[2,1,5],[3,3,7]], capacity = 5
输出:true

示例 3:

输入:trips = [[2,1,5],[3,5,7]], capacity = 3
输出:true

示例 4:

输入:trips = [[3,2,7],[3,7,9],[8,3,9]], capacity = 11
输出:true

 

提示:

  • 你可以假设乘客会自觉遵守 “先下后上” 的良好素质
  • trips.length <= 1000
  • trips[i].length == 3
  • 1 <= trips[i][0] <= 100
  • 0 <= trips[i][1] < trips[i][2] <= 1000
  • 1 <= capacity <= 100000

解法

差分数组

Python3

class Solution:
    def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
        delta = [0] * 1001
        for num, start, end in trips:
            delta[start] += num
            delta[end] -= num
        cur = 0
        for num in delta:
            cur += num
            if cur > capacity:
                return False
        return True

Java

class Solution {
    public boolean carPooling(int[][] trips, int capacity) {
        int[] delta = new int[1001];
        for (int[] trip : trips) {
            int num = trip[0], start = trip[1], end = trip[2];
            delta[start] += num;
            delta[end] -= num;
        }
        int cur = 0;
        for (int num : delta) {
            cur += num;
            if (cur > capacity) {
                return false;
            }
        }
        return true;
    }
}

JavaScript

/**
 * @param {number[][]} trips
 * @param {number} capacity
 * @return {boolean}
 */
 var carPooling = function(trips, capacity) {
    let delta = new Array();
    for (let trip of trips) {
        let [num, start, end] = trip;
        delta[start] = (delta[start] || 0) + num;
        delta[end] = (delta[end] || 0) - num;
    }
    let total = 0;
    for (let i = 0; i < delta.length; i++) {
        let cur = delta[i];
        if (cur == undefined) continue;
        total += cur;
        if (total > capacity) return false;
    }
    return true;
};

C++

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

JavaScript

/**
 * @param {number[][]} trips
 * @param {number} capacity
 * @return {boolean}
 */
 var carPooling = function(trips, capacity) {
    let delta = new Array();
    for (let trip of trips) {
        let [num, start, end] = trip;
        delta[start] = (delta[start] || 0) + num;
        delta[end] = (delta[end] || 0) - num;
    }
    let total = 0;
    for (let i = 0; i < delta.length; i++) {
        let cur = delta[i];
        if (cur == undefined) continue;
        total += cur;
        if (total > capacity) return false;
    }
    return true;
};

...