LeetCode 题解工作台

K 次乘运算后的最终数组 I

给你一个整数数组 nums ,一个整数 k 和一个整数 multiplier 。 你需要对 nums 执行 k 次操作,每次操作中: 找到 nums 中的 最小 值 x ,如果存在多个最小值,选择最 前面 的一个。 将 x 替换为 x * multiplier 。 请你返回执行完 k 次乘运算之后,…

category

4

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

简单 · 数组·数学

bolt

答案摘要

我们可以用一个小根堆来维护数组 中的元素,每次从小根堆中取出最小值,将其乘以 后再放回小根堆中。在实现过程中,我们往小根堆插入的是元素的下标,然后自定义比较函数,使得小根堆按照 中元素的大小作为第一关键字,下标作为第二关键字进行排序。 最后,我们返回数组 即可。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·数学 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数数组 nums ,一个整数 k  和一个整数 multiplier 。

你需要对 nums 执行 k 次操作,每次操作中:

  • 找到 nums 中的 最小 值 x ,如果存在多个最小值,选择最 前面 的一个。
  • x 替换为 x * multiplier 。

请你返回执行完 k 次乘运算之后,最终的 nums 数组。

 

示例 1:

输入:nums = [2,1,3,5,6], k = 5, multiplier = 2

输出:[8,4,6,5,6]

解释:

操作 结果
1 次操作后 [2, 2, 3, 5, 6]
2 次操作后 [4, 2, 3, 5, 6]
3 次操作后 [4, 4, 3, 5, 6]
4 次操作后 [4, 4, 6, 5, 6]
5 次操作后 [8, 4, 6, 5, 6]

示例 2:

输入:nums = [1,2], k = 3, multiplier = 4

输出:[16,8]

解释:

操作 结果
1 次操作后 [4, 2]
2 次操作后 [4, 8]
3 次操作后 [16, 8]

 

提示:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100
  • 1 <= k <= 10
  • 1 <= multiplier <= 5
lightbulb

解题思路

方法一:优先队列(小根堆)+ 模拟

我们可以用一个小根堆来维护数组 nums\textit{nums} 中的元素,每次从小根堆中取出最小值,将其乘以 multiplier\textit{multiplier} 后再放回小根堆中。在实现过程中,我们往小根堆插入的是元素的下标,然后自定义比较函数,使得小根堆按照 nums\textit{nums} 中元素的大小作为第一关键字,下标作为第二关键字进行排序。

最后,我们返回数组 nums\textit{nums} 即可。

时间复杂度 O((n+k)×logn)O((n + k) \times \log n),空间复杂度 O(n)O(n)。其中 nn 是数组 nums\textit{nums} 的长度。

1
2
3
4
5
6
7
8
9
10
class Solution:
    def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
        pq = [(x, i) for i, x in enumerate(nums)]
        heapify(pq)
        for _ in range(k):
            _, i = heappop(pq)
            nums[i] *= multiplier
            heappush(pq, (nums[i], i))
        return nums
speed

复杂度分析

指标
时间O(N + k \cdot \log N)
空间O(N)
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for the candidate's ability to handle the array transformations efficiently with a priority queue.

  • question_mark

    Check if the candidate understands the trade-offs of using a priority queue and its logarithmic time complexity.

  • question_mark

    Evaluate the candidate's approach to maintaining the array's sorted order throughout multiple operations.

warning

常见陷阱

外企场景
  • error

    Overcomplicating the solution by trying to manually sort the array after each operation, which increases time complexity.

  • error

    Failing to account for the fact that the array size can vary up to 100 elements, making inefficient solutions impractical.

  • error

    Not correctly updating the priority queue after each operation, leading to incorrect final results.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Increasing the number of operations k beyond the usual limits and testing if the approach still handles it efficiently.

  • arrow_right_alt

    Changing the multiplier value dynamically during the operations instead of keeping it constant.

  • arrow_right_alt

    Testing with different initial array sizes and ensuring the priority queue implementation scales well.

help

常见问题

外企场景

K 次乘运算后的最终数组 I题解:数组·数学 | LeetCode #3264 简单