闯关leetcode——3264. Final Array State After K Multiplication Operations I

题目

地址

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:

OperationResult
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:

OperationResult
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;
    }
};

在这里插入图片描述

代码地址

https://github.com/f304646673/leetcode/tree/main/3264-Final-Array-State-After-K-Multiplication-Operations-I/cplusplus

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

breaksoftware

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值