LeetCode 1094. Car Pooling
原题链接在这里: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 trips
, trip[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:
trips.length <= 1000
trips[i].length == 3
1 <= trips[i][0] <= 100
0 <= trips[i][1] < trips[i][2] <= 1000
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;
}
}
LeetCode 1094. Car Pooling的更多相关文章
- 【leetcode】1094. Car Pooling
题目如下: You are driving a vehicle that has capacity empty seats initially available for passengers. T ...
- 【LeetCode】1094. Car Pooling 拼车
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 差分数组 代码 日期 题目地址:https://le ...
- LeetCode Meeting Rooms II
原题链接在这里:https://leetcode.com/problems/meeting-rooms-ii/ Given an array of meeting time intervals con ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode.接雨水
题外话:LeetCode上一个测试用例总是通不过(我在文章末贴出通不过的测试用例),给的原因是超出运行时间,我拿那个测试用例试了下2.037ms运行完.我自己强行给加了这句: && m ...
- LeetCode Top 100 Liked 点赞最高的 100 道算法题
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:刷题顺序,刷题路径,好题,top100,怎么刷题,Leet ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
随机推荐
- Centos.安装Goaccess1.2
下载 wget http://tar.goaccess.io/goaccess-1.2.tar.gz 解压 tar -xzvf goaccess-1.2.tar.gz 安装支持库GeoIP-devel ...
- C#原型模式(深拷贝、浅拷贝)
原型模式就是用于创建重复的对象,当想要创建一个新的对象但是开销比较大或者想将对象的当前状态保存下来的时候,我们就可以使用原型模式. 创建原型 public abstract class Base { ...
- Windows / Office - KMS激活
Windows / Office - KMS激活 支持Windows操作系统,支持Office软件:包括Windows 10,Office 2016:包括VL版本和MSDN版. (UPDATE: Of ...
- hbase 查看元数据
package com.jason.lala.pipe.dbinfo import com.jason.lala.common.query.option.HbaseOptions import org ...
- vertica单节点故障恢复 Startup Failed, ASR Required
测试环境的vertica是单节点的,无法做到故障自动恢复,需要手工处理.案例如下: 发现5433端口连接不上,vertica挂了,手工运行admintools,重新启动vertica,仍然失败,提示: ...
- [转帖]龙芯3A4000处理器实测:28nm工艺不变 性能仍可提升100%以上
龙芯3A4000处理器实测:28nm工艺不变 性能仍可提升100%以上 http://news.mydrivers.com/1/663/663122.htm 龙芯是中科院下属的计算机所研发的自主产权国 ...
- CF1200D 【White Lines】
退役快一年了之后又打了场紧张刺激的$CF$(斜眼笑) 然后发现$D$题和题解里的大众做法不太一样 (思路清奇) 题意不再赘述,我们可以看到这个题~~好做~~在只有一次擦除机会,尝试以此为突破口解决问题 ...
- 02、策略模式(Strategy)
一.概念: 策略是为达到某一目的而采取的手段或方法,策略模式的本质是目标与手段的分离, 手段不同而最终达成的目标一致.客户只关心目标而不在意具体的实现方法, 实现方法要根据具体的环境因素而变化. 二. ...
- SQLServer之服务器连接
目录 SQL Server Management Studio连接 CMD命令行窗口连接 通用数据连接文件连接 SQL Server Management Studio连接 定义 SQL Server ...
- beyond compare全文件夹比较,仅显示变化的文件
beyond compare是一款非常优秀的文件夹同步比较工具,赞. 非常强大的一点就是给定两个文件夹可以自动列出所有不同的文件和子文件夹,但是有一点可能很多人碰到过,也就是需要一个个点开才能重新比 ...