853 Car Fleet 車隊
Description:
There are n cars going to the same destination along a one-lane road. The destination is target miles away.
You are given two integer array position and speed, both of length n, where position[i] is the position of the ith car and speed[i] is the speed of the ith car (in miles per hour).
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 (i.e., 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.
Return the number of car fleets that will arrive at the destination.
Example:
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.
Example 2:
Input: target = 10, position = [3], speed = [3]
Output: 1
Constraints:
n == position.length == speed.length
1 <= n <= 10^5
0 < target <= 10^6
0 <= position[i] < target
All the values of position are unique.
0 < speed[i] <= 10^6
題目描述:
N 輛車沿著一條車道駛向位于 target 英里之外的共同目的地。
每輛車 i 以恒定的速度 speed[i] (英里/小時),從初始位置 position[i] (英里) 沿車道駛向目的地杉允。
一輛車永遠不會超過前面的另一輛車,但它可以追上去,并與前車以相同的速度緊接著行駛财岔。
此時吞杭,我們會忽略這兩輛車之間的距離,也就是說俘种,它們被假定處于相同的位置秤标。
車隊 是一些由行駛在相同位置、具有相同速度的車組成的非空集合宙刘。注意苍姜,一輛車也可以是一個車隊。
即便一輛車在目的地才趕上了一個車隊悬包,它們仍然會被視作是同一個車隊衙猪。
會有多少車隊到達目的地?
示例 :
輸入:target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
輸出:3
解釋:
從 10 和 8 開始的車會組成一個車隊,它們在 12 處相遇。
從 0 處開始的車無法追上其它車垫释,所以它自己就是一個車隊丝格。
從 5 和 3 開始的車會組成一個車隊,它們在 6 處相遇棵譬。
請注意显蝌,在到達目的地之前沒有其它車會遇到這些車隊,所以答案是 3订咸。
提示:
0 <= N <= 10 ^ 4
0 < target <= 10 ^ 6
0 < speed[i] <= 10 ^ 6
0 <= position[i] < target
所有車的初始位置各不相同曼尊。
思路:
先按照 position 排序
然后計算各車到達 target 的時間
用單調棧或者從后往前遍歷時間數組
時間較少的可以組合成一個車隊
時間復雜度為 O(nlgn), 空間復雜度為 O(n)
代碼:
C++:
class Solution
{
public:
int carFleet(int target, vector<int>& position, vector<int>& speed)
{
map<int, int> cars;
for (int i = 0; i < position.size(); i++) cars[position[i]] = speed[i];
stack<float> s;
for (auto& [position, v] : cars)
{
float time = float(target - position) / v;
while (!s.empty() and time >= s.top()) s.pop();
s.push(time);
}
return s.size();
}
};
Java:
class Solution
{
public int carFleet(int target, int[] position, int[] speed)
{
TreeMap<Integer, Integer> cars = new TreeMap<>();
for (int i = 0; i < position.length; i++) cars.put(position[i], speed[i]);
Stack<Double> stack = new Stack<>();
for (Map.Entry<Integer, Integer> entry : cars.entrySet()) {
double time = ((double)(target - entry.getKey())) / entry.getValue();
while (!stack.empty() && time >= stack.peek()) stack.pop();
stack.push(time);
}
return stack.size();
}
}
Python:
class Solution:
def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
cars = sorted(zip(position, speed))
times, result = [float(target - p) / s for p, s in cars], 0
while len(times) > 1:
lead = times.pop()
if lead < times[-1]:
result += 1
else:
times[-1] = lead
return result + bool(times)