-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3264-Final_State.cpp
More file actions
31 lines (26 loc) · 1.01 KB
/
3264-Final_State.cpp
File metadata and controls
31 lines (26 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
int len = nums.size();
// Min-heap priority queue for pairs
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> p;
// Push all elements into the priority queue with their indices
for (int i = 0; i < len; i++) {
p.push({nums[i], i});
}
// Perform the k operations
for (int i = 0; i < k; i++) {
auto [x, y] = p.top(); // Decompose the top element
p.pop();
x = x * multiplier; // Multiply the value
p.push({x, y}); // Push the updated value back with the index
}
// Update the nums array based on the modified priority queue
while (!p.empty()) {
auto [x, y] = p.top(); // Decompose the top element
nums[y] = x; // Update the corresponding index in nums
p.pop();
}
return nums;
}
};