codekofi
← All problems

Problem 76

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

1vector<vector<int>> solution(vector<vector<int>> spans) {
2
3 if (spans.empty()) {
4 return {};
5 }
6
7 sort(spans.begin(), spans.end());
8
9 vector<vector<int>> ans;
10
11 int start = spans[0][0];
12 int end = spans[0][1];
13
14 for (int i = 1; i < (int)spans.size(); i++) {
15
16 if (spans[i][0] <= end) {
17 end = max(end, spans[i][1]);
18 } else {
19 ans.push_back({start, end});
20 start = spans[i][0];
21 end = spans[i][1];
22 }
23 }
24
25 ans.push_back({start, end});
26
27 return ans;
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.