codekofi
← All problems

Problem 110

Medium

1 · Worked examples

0 / 3

Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 3 outputs correctly.

The accepted solution

1int solution(int target, const vector<int>& start,
2 const vector<int>& speed) {
3
4 int n = start.size();
5 vector<pair<int, double>> cars(n);
6
7 for (int i = 0; i < n; i++) {
8
9 double time = double(target - start[i]) / speed[i];
10
11 cars[i] = {start[i], time};
12 }
13
14 sort(cars.begin(), cars.end());
15
16 int groups = 0;
17 double slowest = 0;
18
19 for (int i = n - 1; i >= 0; i--) {
20
21 if (cars[i].second > slowest) {
22 groups++;
23 slowest = cars[i].second;
24 }
25 }
26
27 return groups;
28}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.