zoukankan      html  css  js  c++  java
  • [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 个空座位可以用来载客。由于道路的限制,车 只能 向一个方向行驶(也就是说,不允许掉头或改变方向,你可以将其想象为一个向量)。

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

    必须接送的乘客数量;
    乘客的上车地点;
    以及乘客的下车地点。
    这些给出的地点位置是从你的 初始 出发位置向前行驶到这些地点所需的距离(它们一定在你的行驶方向上)。

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

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/car-pooling
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这道题的糖衣出的很巧妙,但是看过之后居然发现核心思路其实是扫描线。具体的思路是,因为题目条件限制了trips的长度不大于1000,所以创建一个长度为1001的数组,然后扫描这个trips数组。对于每一个trip,从他的起点start_location到他的终点end_location,把当前这个trip涉及到的乘客数量加到数组里。在遍历的过程中,如果有任何一个location上的乘客人数大于capacity,则返回false。否则遍历完trips数组就返回true。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public boolean carPooling(int[][] trips, int capacity) {
     3         int[] allTrip = new int[1001];
     4         for (int i = 0; i < trips.length; i++) {
     5             for (int j = trips[i][1]; j < trips[i][2]; j++) {
     6                 allTrip[j] += trips[i][0];
     7                 if (allTrip[j] > capacity) {
     8                     return false;
     9                 }
    10             }
    11         }
    12         return true;
    13     }
    14 }

    另一种方法跟1109题类似,也是用一个数组counter记录每个位置相比于前一个位置上的差值,最后再把counter数组累加一遍。如果累加的过程中,有任何一个位置上的乘客数量大于capacity,则返回false。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public boolean carPooling(int[][] trips, int capacity) {
     3         int[] counter = new int[1001];
     4         for (int i = 0; i < trips.length; i++) {
     5             counter[trips[i][1]] += trips[i][0];
     6             counter[trips[i][2]] -= trips[i][0];
     7         }
     8         int cap = 0;
     9         for (int i = 0; i < 1001; i++) {
    10             cap += counter[i];
    11             if (cap > capacity) {
    12                 return false;
    13             }
    14         }
    15         return true;
    16     }
    17 }

    另一种实现

     1 class Solution {
     2     public boolean carPooling(int[][] trips, int capacity) {
     3         int[] counter = new int[1001];
     4         int n = counter.length;
     5         for (int[] t : trips) {
     6             counter[t[1]] += t[0];
     7             if (t[2] < n) {
     8                 counter[t[2]] -= t[0];
     9             }
    10         }
    11         for (int i = 1; i < counter.length; i++) {
    12             counter[i] += counter[i - 1];
    13             if (counter[i] > capacity) {
    14                 return false;
    15             }
    16         }
    17         return true;
    18     }
    19 }

    相关题目

    1094. Car Pooling

    1109. Corporate Flight Bookings

    扫描线相关题目

    LeetCode 题目总结

  • 相关阅读:
    on、where、having的区别(转载)
    Javascript 中的非空判断 undefined,null, NaN的区别
    SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
    ibatis通过Map封装参数调用存储过程
    NoSQL架构实践
    js实现密码强度验证
    ubuntu 10.04安装qtcreator并汉化
    2017第19周一
    越挫越战,越战越勇
    2017第18周六
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13630074.html
Copyright © 2011-2022 走看看