codekofi
← All problems

Problem 109

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(const vector<vector<int>>& points,
2 int k) {
3
4 int n = points.size();
5 priority_queue<pair<int, int>> far;
6
7 for (int i = 0; i < n; i++) {
8
9 int x = points[i][0];
10 int y = points[i][1];
11
12 far.push({x * x + y * y, i});
13
14 if ((int)far.size() > k) {
15 far.pop();
16 }
17 }
18
19 vector<int> ids;
20
21 while (!far.empty()) {
22 ids.push_back(far.top().second);
23 far.pop();
24 }
25
26 sort(ids.begin(), ids.end());
27
28 vector<vector<int>> ans;
29
30 for (int i : ids) {
31 ans.push_back(points[i]);
32 }
33
34 return ans;
35}

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.