Car Pooling

LintCode Q 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 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

Solution

Solution 1 : Using PriorityQueue (not efficient)

Code:

public boolean carPooling(int[][] trips, int capacity) {
  Queue<int[]> pq = new PriorityQueue<>(
    (a, b) -> {
      if (a[1] != b[1])
        return a[1] - b[1];
      return a[2] - b[2];
    }
  );

  for (int[] trip: trips) pq.offer(trip);

  while (!pq.isEmpty()) {
    int[] curr = pq.poll();
    
    if (curr[0] > capacity) return false;
    
    if (pq.isEmpty()) return curr[0] <= capacity;
    
    int[] next = pq.peek();
    if (next[0] > capacity) return false;
    
    if (next[1] < curr[2] && next[2] <= curr[2]) {
      pq.poll(); 
      if (next[0] + curr[0] > capacity) return false;
      pq.offer(new int[]{next[0] + curr[0], next[1], next[2]});
      pq.offer(new int[]{curr[0], next[2], curr[2]});
    }
    
    if (next[1] < curr[2] && next[2] > curr[2]) {
      pq.poll(); 
      if (next[0] + curr[0] > capacity) return false;
      pq.offer(new int[]{next[0] + curr[0], next[1], curr[2]});
      pq.offer(new int[]{next[0], curr[2], next[2]});
    }
  }

  return true;
}

Solution 2 : Using TreeMap

Code:

public boolean carPooling(int[][] trips, int capacity) {
  Map<Integer, Integer> map = new TreeMap<>();
  for (int[] trip: trips) {
    map.put(trip[1], map.getOrDefault(trip[1], 0) + trip[0]);
    map.put(trip[2], map.getOrDefault(trip[2], 0) - trip[0]);
  }

  int count = 0;
  for (int stop: map.keySet()) {
    count += map.get(stop);
    if (count > capacity) return false;
  }

  return true;
}

Solution 3 : Using Array

Since trips.length <= 1000, we can use an array instead of an ordered map (treemap).

Code:

public boolean carPooling(int[][] trips, int capacity) {
  int[] stops = new int[1001];

  for (int[] trip: trips) {
    stops[trip[1]] += trip[0];
    stops[trip[2]] -= trip[0];
  }

  int count = 0;
  for (int stop: stops) {
    count += stop;
    if (count > capacity) return false;
  }

  return true;
}

   Reprint policy


《Car Pooling》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
  TOC