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 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:

 1 class Solution {
 2     public boolean carPooling(int[][] trips, int capacity) {
 3         if(trips == null || trips.length == 0){
 4             return true;
 5         }
 6         
 7         List<int []> list = new ArrayList<>();
 8         for(int [] trip : trips){
 9             list.add(new int[]{trip[1], trip[0]});
10             list.add(new int[]{trip[2], -trip[0]});
11         }
12         
13         Collections.sort(list, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
14         int count = 0;
15         for(int [] arr : list){
16             count += arr[1];
17             if(count > capacity){
18                 return false;
19             }
20         }
21         
22         return true;
23     }
24 }

Could use minHeap to record the trip based on destination.

For each trip in the sorted trips, poll the previous trips with earlier destination.

Time Complexity: O(nlogn).

Space: O(n).

AC Java:

 1 class Solution {
 2     public boolean carPooling(int[][] trips, int capacity) {
 3         if(trips == null || trips.length == 0){
 4             return true;
 5         }
 6         
 7         Arrays.sort(trips, (a, b) -> a[1] == b[1] ? a[2] - b[2] : a[1] - b[1]);
 8         PriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b) -> a[2] - b[2]);
 9         int count = 0;
10         for(int [] trip : trips){
11             while(!minHeap.isEmpty() && minHeap.peek()[2] <= trip[1]){
12                 int [] cur = minHeap.poll();
13                 count -= cur[0];
14             }
15             
16             minHeap.add(trip);
17             count += trip[0];
18             if(count > capacity){
19                 return false;
20             }
21         }
22         
23         return true;
24     }
25 }

类似Meeting Rooms II.

posted @ 2019-12-11 13:14  Dylan_Java_NYC  阅读(525)  评论(0编辑  收藏  举报