题目
地址
https://leetcode.com/problems/final-array-state-after-k-multiplication-operations-i/description/
内容
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.
Example 1:
Input: nums = [2,1,3,5,6], k = 5, multiplier = 2
Output: [8,4,6,5,6]
Explanation:
Operation | Result |
---|---|
After operation 1 | [2, 2, 3, 5, 6] |
After operation 2 | [4, 2, 3, 5, 6] |
After operation 3 | [4, 4, 3, 5, 6] |
After operation 4 | [4, 4, 6, 5, 6] |
After operation 5 | [8, 4, 6, 5, 6] |
Example 2:
Input: nums = [1,2], k = 3, multiplier = 4
Output: [16,8]
Explanation:
Operation | Result |
---|---|
After operation 1 | [4, 2] |
After operation 2 | [4, 8] |
After operation 3 | [16, 8] |
Constraints:
- 1 <= nums.length <= 100
- 1 <= nums[i] <= 100
- 1 <= k <= 10
- 1 <= multiplier <= 5
解题
这题是要从一个数组中,找到最小的那个数(入股相同大小,则取第一个最小的数),然后乘以multiplier并放会数组中。这样的循环执行k次。
这题没太多技巧性,主要考察的是对C++标准库的熟悉。我们可以有两种解法。
一种是按照上述逻辑,使用标准库中的方法,找到最小数的下标,然后修改它。
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
vector<int> result(nums);
for (int i = 0; i < k; i++) {
auto minElementIter = min_element(result.begin(), result.end());
*minElementIter *= multiplier;
}
return result;
}
}
这儿使用了C++17引入的min_element。它的返回值正好符合题目的要求——如果同样小,返回第一个。
Iterator to the smallest element in the range [first, last). If several elements in the range are equivalent to the smallest element, returns the iterator to the first such element. Returns last if the range is empty.
另外一个思路是使用小顶堆。在C++的标准库中,priority_queue是堆的思路实现的,但是它是大顶堆。我们可以通过传递一个比较函数(greater),来让其转换为小顶堆。
using pii = pair<int, int>;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pair类型的比较也正好符合题目的要求——如果同样小,返回第一个。
Compares lhs and rhs lexicographically by operator<, that is, compares the first elements and only if they are equivalent, compares the second elements. The behavior is undefined if the type and value category of any of lhs.first < rhs.first, rhs.first < lhs.first, or lhs.second < rhs.second do not meet the BooleanTestable requirements.
然后我们构建小顶堆,这样最小的数就位于顶部。
for (int i = 0; i < nums.size(); i++) {
pq.push({nums[i], i});
}
我们将最小的数抛出,然后修改完后再放入堆中,这个时候堆会自动做调整。
while (k--) {
auto [value, index] = pq.top();
pq.pop();
pq.push({value * multiplier, index});
}
最后我们再将结果按照之前的下标重构。
vector<int> result(nums.size());
while (!pq.empty()) {
auto [value, index] = pq.top();
pq.pop();
result[index] = value;
}
return result;
完整代码如下
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
class Solution {
public:
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
using pii = pair<int, int>;
priority_queue<pii, vector<pii>, greater<pii>> pq;
for (int i = 0; i < nums.size(); i++) {
pq.push({nums[i], i});
}
while (k--) {
auto [value, index] = pq.top();
pq.pop();
pq.push({value * multiplier, index});
}
vector<int> result(nums.size());
while (!pq.empty()) {
auto [value, index] = pq.top();
pq.pop();
result[index] = value;
}
return result;
}
};