原题链接在这里: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.

最新文章

  1. 百度地图API 批量添加 带检索功能的信息窗口
  2. MFC如何隐藏RibbonBar的QAT QuickAccessToolBar(快速访问工具栏)
  3. HTML5 UI框架Kendo UI Web教程:创建自定义组件(三)
  4. svn更新项目时遇到被锁住的问题
  5. STM32F4_TIM输出PWM波形(可调频率、占空比)
  6. React可控组件与不可控组件
  7. 用 SQL 脚本读取Excel 中的sheet数量及名称
  8. C语言运算符的注意问题
  9. 读书笔记: 深入浅出node.js
  10. centos 7安装pycharm
  11. Kali学习笔记4:Wireshark详细使用方法
  12. MySQL拓展操作
  13. Ubuntu中,wxpython的TextCtrl引发的error:_pixman_log_error
  14. Linux中断管理 (2)软中断和tasklet
  15. jenkins服务器上安装配置Android SDK
  16. MVC模式和MVP模式的区别
  17. 2018-10-19,下午4点拿到京东offer
  18. Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 拒绝访问
  19. RTP/RTCP 和 SRTP/SRTCP协议(转)
  20. 微信小程序- wx.request请求不到数据

热门文章

  1. gcc/g++ -O 优化选项说明
  2. 如何解决macbook pro摄像头不工作的问题
  3. ZYNQ笔记(2):PS端——Hello World !
  4. Python数据库添加时间
  5. KVM虚拟机网络配置 Bridge方式,NAT方式
  6. instr函数的用法
  7. Java程序员需要掌握的技能
  8. JDK相关目录介绍
  9. Python Lab Assignments
  10. nginx配置不当容易产生的安全问题