Skip to content

Latest commit

 

History

History
134 lines (119 loc) · 3.7 KB

1094. Car Pooling.md

File metadata and controls

134 lines (119 loc) · 3.7 KB

leetcode Daily Challenge on September 21th, 2020.


Difficulty : Medium

Related Topics : Greedy


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

  • mine
    • Java
      • Runtime: 5 ms, faster than 66.40%, Memory Usage: 39.2 MB, less than 65.62% of Java online submissions
        // O(N * logN)time
        // O(N)space
        public boolean carPooling(int[][] trips, int capacity) {
            Arrays.sort(trips, (t1, t2) -> t1[1] - t2[1]);
            int count = 0;
            PriorityQueue<int[]> queue = new PriorityQueue<>((o1, o2) -> o1[2] - o2[2]);
            for (int[] t : trips) {
                int[] temp;
                while (!queue.isEmpty() && (temp = queue.peek())[2] <= t[1]) {
                    count -= temp[0];
                    queue.poll();
                }
                count += t[0];
                if (count > capacity) return false;
        
                queue.add(t);
            }
            return true;
        }
        

  • leetcode solution
  • Runtime: 5 ms, faster than 66.40%, Memory Usage: 39.2 MB, less than 69.55% of Java online submissions

    // O(N*logN)time
    // O(N)space
    public boolean carPooling(int[][] trips, int capacity) {
        Map<Integer, Integer> timestamp = new TreeMap<>();
        for (int[] trip : trips) {
            int start_passenger = timestamp.getOrDefault(trip[1], 0) + trip[0];
            timestamp.put(trip[1], start_passenger);
    
            int end_passenger = timestamp.getOrDefault(trip[2], 0) - trip[0];
            timestamp.put(trip[2], end_passenger);
        }
        int ued_capacity = 0;
        for (int passenger_change : timestamp.values()) {
            ued_capacity += passenger_change;
            if (ued_capacity > capacity) {
                return false;
            }
        }
        return true;
    }
    
  • Runtime: 1 ms, faster than 99.28%, Memory Usage: 39.6 MB, less than 36.80% of Java online submissions

    // O(max(N, 1001))time
    // O(1001)space
    public boolean carPooling(int[][] trips, int capacity) {
        int[] timestamp = new int[1001];
        for (int[] trip : trips) {
            timestamp[trip[1]] += trip[0];
            timestamp[trip[2]] -= trip[0];
        }
        int ued_capacity = 0;
        for (int number : timestamp) {
            ued_capacity += number;
            if (ued_capacity > capacity) {
                return false;
            }
        }
        return true;
    }