3264. Final Array State After K Multiplication Operations I LeetCode Solution

In this guide, you will get 3264. Final Array State After K Multiplication Operations I LeetCode Solution with the best time and space complexity. The solution to Final Array State After K Multiplication Operations I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Final Array State After K Multiplication Operations I solution in C++
  4. Final Array State After K Multiplication Operations I solution in Java
  5. Final Array State After K Multiplication Operations I solution in Python
  6. Additional Resources
3264. Final Array State After K Multiplication Operations I LeetCode Solution image

Problem Statement of 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.

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

Complexity Analysis

  • Time Complexity: O(n + k\log n)
  • Space Complexity: O(n)

3264. Final Array State After K Multiplication Operations I LeetCode Solution in C++

class Solution {
 public:
  vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
    vector<int> ans(nums.size());
    using P = pair<int, int>;  // (nums[i], i)
    priority_queue<P, vector<P>, greater<>> minHeap;

    for (int i = 0; i < nums.size(); ++i)
      minHeap.emplace(nums[i], i);

    while (k-- > 0) {
      const auto [num, i] = minHeap.top();
      minHeap.pop();
      minHeap.emplace(num * multiplier, i);
    }

    while (!minHeap.empty()) {
      const auto [num, i] = minHeap.top();
      minHeap.pop();
      ans[i] = num;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

3264. Final Array State After K Multiplication Operations I LeetCode Solution in Java

class Solution {
  public int[] getFinalState(int[] nums, int k, int multiplier) {
    int[] ans = new int[nums.length];
    // (nums[i], i)
    Queue<Pair<Integer, Integer>> minHeap = new PriorityQueue<>(
        new PriorityQueue<>(Comparator.comparing(Pair<Integer, Integer>::getKey)
                                .thenComparing(Pair<Integer, Integer>::getValue)));

    for (int i = 0; i < nums.length; ++i)
      minHeap.offer(new Pair<>(nums[i], i));

    while (k-- > 0) {
      final int num = minHeap.peek().getKey();
      final int i = minHeap.poll().getValue();
      minHeap.offer(new Pair<>(num * multiplier, i));
    }

    while (!minHeap.isEmpty()) {
      final int num = minHeap.peek().getKey();
      final int i = minHeap.poll().getValue();
      ans[i] = num;
    }

    return ans;
  }
}
// code provided by PROGIEZ

3264. Final Array State After K Multiplication Operations I LeetCode Solution in Python

class Solution:
  def getFinalState(
      self,
      nums: list[int],
      k: int,
      multiplier: int
  ) -> list[int]:
    ans = [0] * len(nums)
    minHeap = [(num, i) for i, num in enumerate(nums)]
    heapq.heapify(minHeap)

    for _ in range(k):
      num, i = heapq.heappop(minHeap)
      heapq.heappush(minHeap, (num * multiplier, i))

    for num, i in minHeap:
      ans[i] = num

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.