codekofi
← All problems

Problem 92

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(vector<vector<int>> spans) {
2
3 if (spans.empty()) {
4 return 0;
5 }
6
7 sort(spans.begin(), spans.end(),
8 [](const vector<int>& a, const vector<int>& b) {
9 return a[1] < b[1];
10 });
11
12 int kept = 0;
13 int edge = INT_MIN;
14
15 for (const auto& s : spans) {
16
17 if (s[0] >= edge) {
18 kept++;
19 edge = s[1];
20 }
21 }
22
23 int total = spans.size();
24
25 return total - kept;
26}

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.