Maximize Sum Of Array After K Negations LeetCode solution in C++

1
class Solution {
2
public:
3
int largestSumAfterKNegations(vector<int> &A, int k) {
4
priority_queue<int, vector<int>, greater<int>> pq(A.begin(), A.end());
5
while (k--) {
6
int t = pq.top();
7
pq.pop();
8
pq.push(t * -1);
9
}
10

11
int n = 0;
12
while (!pq.empty()) {
13
int t = pq.top();
14
pq.pop();
15
n += t;
16
}
17
return n;
18
}
19
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0