2025-08-24 09:29:48 +0000 UTC

Final Array State After K Multiplication Operations I

Code

class Solution:
    def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
        pos, n = defaultdict(list), len(nums)
        hp = nums.copy()
        heapq.heapify(hp)
        for i in reversed(range(n)):
            heapq.heappush(pos[nums[i]], i)
        while k > 0:
            k -= 1
            val = heapq.heappop(hp)
            new_val = val * multiplier
            heapq.heappush(hp, new_val)
            idx = heapq.heappop(pos[val])
            nums[idx] = new_val
            heapq.heappush(pos[new_val], idx)
        return nums