853.Car-Fleet

853. Car Fleet

题目地址

https://leetcode.com/problems/car-fleet/

题目描述

N cars are going to the same destination along a one lane road.  The destination is target miles away.

Each car i has a constant speed speed[i] (in miles per hour), and initial position position[i] miles towards the target along the road.

A car can never pass another car ahead of it, but it can catch up to it, and drive bumper to bumper at the same speed.

The distance between these two cars is ignored - they are assumed to have the same position.

A car fleet is some non-empty set of cars driving at the same position and same speed.  Note that a single car is also a car fleet.

If a car catches up to a car fleet right at the destination point, it will still be considered as one car fleet.

How many car fleets will arrive at the destination?

Example 1:
Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
Output: 3
Explanation:
The cars starting at 10 and 8 become a fleet, meeting each other at 12.
The car starting at 0 doesn't catch up to any other car, so it is a fleet by itself.
The cars starting at 5 and 3 become a fleet, meeting each other at 6.
Note that no other cars meet these fleets before the destination, so the answer is 3.

Note:

0 <= N <= 10 ^ 4
0 < target <= 10 ^ 6
0 < speed[i] <= 10 ^ 6
0 <= position[i] < target
All initial positions are different.

代码

Approach #1 Sort

Time: O(N log N) && Space: O(N)

class Solution {
  public int carFleet(int target, int[] position, int[] speed) {
        int N = position.length;
    if (N == 0)        return 0;
    Car[] cars = new Car[N];
    for (int i = 0; i < N; i++) {
      cars[i] = new Car(position[i], (double)(target - position[i]) / speed[i]);
    }

    Arrays.sort(cars, (a, b) -> Integer.compare(a.position, b.position));

    int ans = 0;
    int t = N;
    while (--t > 0) { // t == 1
      if (cars[t].time < cars[t-1].time) {
        ans++; // if cars[t] arrives sooner, it can't be caught
      } else {
        cars[t-1] = cars[t]; //else, cars[t-1] arrives at same time as cars[t]
      }
    }

    return ans + 1; 
  }
}

public class Car {
  int position;
  double time;
  Car(int p, double t) {
    position = p;
    time = t;
  }
}

Approach #2 Priority Queue

class Solution {
  public int carFleet(int target, int[] position, int[] speed) {
    PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> {
      return b[1] - a[1];
    });

    for (int i = 0; i < position.length; i++) {
      pq.offer(new int[]{i, position[i]});
    }

    double time = 9;
    int count = 0;
    while (!pq.isEmpty()) {
      int[] next = pq.poll();
      int index = next[0];
      int pos = next[1];
      int spd = speed[index];
      double needTime = (double) (target - pos) / spd;
      if (needTime > time) {
        count++;
        time = needTime;
      }
    }

    return count;
  }
}

Approach #3 TreeMap

class Solution {
  public int carFleet(int target, int[] position, int[] speed) {
    TreeMap<Integer, Integer> map = new TreeMap();
    int n = position.length;
    for (int i = 0; i < n; i++) {
      map.put(target - position[i], speed[i]);
    }
    int count = 0;
       double r = -1.0;

    for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
      int d = entry.getKey();        // distance
      int s = entry.getValue();        // speed
      double t = 1.0 * d / s;
      if (t > r) { // this car is unable to catch up previous one, form a new group and update the value
        ++count;
        r = t;
      }
    }

    return count;
  }
}

Last updated