[LeetCode] Final Array State After K Multiplication Operations I

3264. Final Array State After K Multiplication Operations I

You are given an integer array nums, an integer k, and an integer multiplier.

You need to perform k operations on nums. In each operation:

  • Find the minimum value x in nums. If there are multiple occurrences of the minimum value, select the one that appears first.
  • Replace the selected minimum value x with x * multiplier.

Return an integer array denoting the final state of nums after performing all k operations.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
multiset<pair<int,int>> ms;
for(int i = 0; i < nums.size(); i++) ms.insert({nums[i], i});
while(k--) {
auto [val, idx] = *begin(ms); ms.erase(begin(ms));
ms.insert({nums[idx] = val * multiplier, idx});
}
return nums;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/08/25/PS/LeetCode/final-array-state-after-k-multiplication-operations-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.