|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: medium |
| 4 | +# Follow `Topics` tags |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Stack |
| 8 | + - Sorting |
| 9 | + - Monotonic Stack |
| 10 | +--- |
| 11 | + |
| 12 | +# [853. Car Fleet](https://leetcode.com/problems/car-fleet/description/) |
| 13 | + |
| 14 | +## Description |
| 15 | + |
| 16 | +There are **n** cars, each located at a certain distance from the starting point (mile 0), and all are headed toward a common destination at a specified target mile. |
| 17 | + |
| 18 | +You’re given two integer arrays, `position` and `speed`, both of length **n**: |
| 19 | +- `position[i]` indicates the starting mile of the **i-th** car. |
| 20 | +- `speed[i]` represents its speed in miles per hour. |
| 21 | + |
| 22 | +Cars cannot overtake one another, but a faster car can catch up to a slower one. When this happens, it forms a **car fleet**, where all cars move together at the speed of the slowest car in that group. |
| 23 | + |
| 24 | +If a car reaches the target at the same time as a fleet, it joins that fleet, even if it arrives exactly at the destination. |
| 25 | + |
| 26 | +Return the **total number of car fleets** that will arrive at the destination. |
| 27 | + |
| 28 | +**Example 1:** |
| 29 | +``` |
| 30 | +Input: target = 13, position = [9,8,0,5], speed = [1,3,4,1] |
| 31 | +Output: 2 |
| 32 | +Explanation: |
| 33 | +* The cars starting at 9 (speed 1) and 8 (speed 3) become a fleet, meeting each other at 13. The fleet forms at target. |
| 34 | +* The car starting at 0 (speed 4) and 5 (speed 1) become a flleet, meeting each other at 13. |
| 35 | +``` |
| 36 | + |
| 37 | +**Example 2:** |
| 38 | +``` |
| 39 | +Input: target = 11, position = [2], speed = [3] |
| 40 | +Output: 1 |
| 41 | +Explanation: There is only one car, hence there is only one fleet. |
| 42 | +``` |
| 43 | + |
| 44 | +**Constraints:** |
| 45 | + |
| 46 | +* `n == position.length == speed.length` |
| 47 | +* `1 <= n <= 10^5` |
| 48 | +* `0 < target <= 10^6` |
| 49 | +* `0 <= position[i] < target` |
| 50 | +* All the values of `position` are unique. |
| 51 | +* `0 < speed[i] <= 10^6` |
| 52 | + |
| 53 | +## Solution |
| 54 | + |
| 55 | +```java |
| 56 | +class Solution { |
| 57 | + public int carFleet(int target, int[] position, int[] speed) { |
| 58 | + Map<Integer, Double> carMap = new TreeMap<>(Collections.reverseOrder()); |
| 59 | + for (int i = 0; i < position.length; i++) { |
| 60 | + carMap.put(position[i], (double) (target - position[i]) / speed[i]); |
| 61 | + } |
| 62 | + int result = 0; |
| 63 | + double current = 0; |
| 64 | + for (double time : carMap.values()) { |
| 65 | + if (time > current) { |
| 66 | + current = time; |
| 67 | + result++; |
| 68 | + } |
| 69 | + } |
| 70 | + return result; |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +```python |
| 76 | +class Solution: |
| 77 | + def carFleet(self, target: int, position: list[int], speed: list[int]) -> int: |
| 78 | + time_to_reach = [ |
| 79 | + float(target - pos) / spd |
| 80 | + for pos, spd in sorted(zip(position, speed), reverse=True) |
| 81 | + ] |
| 82 | + current = result = 0 |
| 83 | + for t in time_to_reach: |
| 84 | + if t > current: |
| 85 | + current = t |
| 86 | + result += 1 |
| 87 | + return result |
| 88 | +``` |
| 89 | + |
| 90 | +## Complexity |
| 91 | + |
| 92 | +- Time complexity: $$O(nlogn)$$ |
| 93 | +<!-- Add time complexity here, e.g. $$O(n)$$ --> |
| 94 | + |
| 95 | +- Space complexity: $$O(n)$$ |
| 96 | +<!-- Add space complexity here, e.g. $$O(n)$$ --> |
0 commit comments