codekofi
← All problems

Problem 27

Easy

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(const vector<int>& weights) {
2
3 priority_queue<int> heap(weights.begin(), weights.end());
4
5 while (heap.size() > 1) {
6
7 int a = heap.top();
8 heap.pop();
9
10 int b = heap.top();
11 heap.pop();
12
13 if (a != b) {
14 heap.push(a - b);
15 }
16 }
17
18 if (heap.empty()) {
19 return 0;
20 }
21
22 return heap.top();
23}

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.