#include <stdlib.h>
typedef struct {
int pos, speed;
} Car;
static int byPosDesc(const void *a, const void *b) {
return ((const Car *)b)->pos - ((const Car *)a)->pos;
}
int carFleet(int target, int *position, int positionSize, int *speed, int speedSize) {
Car *cars = malloc(positionSize * sizeof(Car));
for (int i = 0; i < positionSize; i++)
cars[i] = (Car){position[i], speed[i]};
qsort(cars, positionSize, sizeof(Car), byPosDesc);
int fleets = 0;
double slowest = 0; // arrival time of the fleet ahead
for (int i = 0; i < positionSize; i++) {
double hours = (double)(target - cars[i].pos) / cars[i].speed;
if (hours > slowest) {
fleets++;
slowest = hours;
}
}
free(cars);
return fleets;
}
#include <algorithm>
#include <stack>
#include <vector>
using namespace std;
class Solution {
public:
int carFleet(int target, vector<int> &position, vector<int> &speed) {
stack<float> st;
vector<pair<int, int>> cars;
for (int i = 0; i < position.size(); i++)
cars.push_back({position[i], speed[i]});
sort(cars.begin(), cars.end());
for (int i = cars.size() - 1; i >= 0; i--) {
float hours = (target - cars[i].first) / (float)cars[i].second;
if (!st.empty()) {
if (hours > st.top())
st.push(hours);
} else
st.push(hours);
}
return st.size();
}
};
package main
import "sort"
func carFleet(target int, position []int, speed []int) int {
idx := make([]int, len(position))
for i := range idx {
idx[i] = i
}
sort.Slice(idx, func(a, b int) bool { return position[idx[a]] > position[idx[b]] })
fleets := 0
slowest := 0.0 // arrival time of the fleet ahead
for _, i := range idx {
hours := float64(target-position[i]) / float64(speed[i])
if hours > slowest {
fleets++
slowest = hours
}
}
return fleets
}
import java.util.Arrays;
class Solution {
public int carFleet(int target, int[] position, int[] speed) {
int n = position.length;
Integer[] idx = new Integer[n];
for (int i = 0; i < n; i++)
idx[i] = i;
Arrays.sort(idx, (a, b) -> position[b] - position[a]);
int fleets = 0;
double slowest = 0; // arrival time of the fleet ahead
for (int i : idx) {
double hours = (double) (target - position[i]) / speed[i];
if (hours > slowest) {
fleets++;
slowest = hours;
}
}
return fleets;
}
}
class Solution {
carFleet(target, position, speed) {
const cars = position
.map((p, i) => [p, speed[i]])
.sort((a, b) => b[0] - a[0]);
let fleets = 0;
let slowest = 0; // arrival time of the fleet ahead
for (const [p, s] of cars) {
const hours = (target - p) / s;
if (hours > slowest) {
fleets++;
slowest = hours;
}
}
return fleets;
}
}
class Solution:
def carFleet(self, target: int, position: list[int], speed: list[int]) -> int:
fleets = 0
slowest = 0.0 # arrival time of the fleet ahead
for p, s in sorted(zip(position, speed), reverse=True):
hours = (target - p) / s
if hours > slowest:
fleets += 1
slowest = hours
return fleets
struct Solution;
impl Solution {
fn car_fleet(target: i32, position: Vec<i32>, speed: Vec<i32>) -> i32 {
let mut cars: Vec<(i32, i32)> = position.into_iter().zip(speed).collect();
cars.sort_unstable_by(|a, b| b.0.cmp(&a.0));
let mut fleets = 0;
let mut slowest = 0.0; // arrival time of the fleet ahead
for (p, s) in cars {
let hours = (target - p) as f64 / s as f64;
if hours > slowest {
fleets += 1;
slowest = hours;
}
}
fleets
}
}