原题链接在这里:https://leetcode.com/problems/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

题解:

For each interval, at start, there are passangers getting on, at end, there are passagers getting off.

Then arrange original interval as start with positive integer of passager count, end with negative integer of passage count.

Sort them based on time. Let negative number come first as they get off, could minimize current passager count.

If current passage count > capacity, return false.

Time Complexity: O(nlogn). n = trips.length.

Space: O(n).

AC Java:

 class Solution {
public boolean carPooling(int[][] trips, int capacity) {
if(trips == null || trips.length == 0){
return true;
} List<int []> list = new ArrayList<>();
for(int [] trip : trips){
list.add(new int[]{trip[1], trip[0]});
list.add(new int[]{trip[2], -trip[0]});
} Collections.sort(list, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
int count = 0;
for(int [] arr : list){
count += arr[1];
if(count > capacity){
return false;
}
} return true;
}
}

类似Meeting Rooms II.

LeetCode 1094. Car Pooling的更多相关文章

  1. 【leetcode】1094. Car Pooling

    题目如下: You are driving a vehicle that has capacity empty seats initially available for passengers.  T ...

  2. 【LeetCode】1094. Car Pooling 拼车

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 差分数组 代码 日期 题目地址:https://le ...

  3. LeetCode Meeting Rooms II

    原题链接在这里:https://leetcode.com/problems/meeting-rooms-ii/ Given an array of meeting time intervals con ...

  4. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  5. LeetCode.接雨水

    题外话:LeetCode上一个测试用例总是通不过(我在文章末贴出通不过的测试用例),给的原因是超出运行时间,我拿那个测试用例试了下2.037ms运行完.我自己强行给加了这句: && m ...

  6. LeetCode Top 100 Liked 点赞最高的 100 道算法题

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:刷题顺序,刷题路径,好题,top100,怎么刷题,Leet ...

  7. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

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

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  9. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

随机推荐

  1. LeetCode 561:数组拆分 I Array Partition I

    文章全部来自公众号:爱写bug 算法是一个程序的灵魂. Given an array of 2n integers, your task is to group these integers into ...

  2. CentOS安装vsftpd FTP后修改默认21端口方法

    第一步:修改配置文件 vi /etc/vsftpd/vsftpd.conf 首先需要在vsftpd配置文件中添加: listen_port=1802pasv_enable=YESpasv_min_po ...

  3. SQLServer查看分区表详细信息

    SQL查看分区内记录个数,常规方法需要知道分区函数然后再显示,网上看到一个一句话显示的方法 ), ps.name ) as partition_scheme, p.partition_number, ...

  4. Tomcat基础操作

    1.在WebApps ROOT目录里,如果删除过ROOT从新创建,放置index.html,index.jsp即可访问. 2.修改默认8080端口,打开server.xml,将8080端口修改为80即 ...

  5. maven的基础入门

    Maven是Java世界中一个很好使的项目管理工具,关于[好使]这个特性从项目的使用量上就能体现出来,虽然说现在有更好使的Gradle,但是Maven的地位也不会那么轻易被撼动,支持者还是多多. Ma ...

  6. java中的对象、类、包、模块、组件、容器、框架、架构的概念入门

    在Java中有那么一些概念:对象.类.包.模块.组件.容器.框架.这些概念都有一个共同的特点,就是[容纳]. 对象(Object) 在Java的世界里,对象是通过属性和方法来分别对应事务所具有的静态属 ...

  7. 【UOJ#33】【UR #2】树上GCD(长链剖分,分块)

    [UOJ#33][UR #2]树上GCD(长链剖分,分块) 题面 UOJ 题解 首先不求恰好,改为求\(i\)的倍数的个数,最后容斥一下就可以解决了. 那么我们考虑枚举一个\(LCA\)位置,在其两棵 ...

  8. 【微信】微信小程序ISO上wx.scanCode BUG

    ================================================== BUG情况: 小程序在onLoad 主动调用wx.scanCode,安卓手机没有问题.iso调用失 ...

  9. Docker 快速安装&搭建 Mysql 环境

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 高级架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...

  10. 大数据Excel导出方案

    static void Main(string[] args) { Excel.Application app = new Excel.Application(); Excel._Workbook r ...