- Define the priority key and whether the smallest or largest item should lead.
- Push each candidate when it becomes eligible.
- Discard stale entries when necessary and process the best live candidate.
Code notes
- 49 lines of Python from the credited upstream file 3266.py.
- The implementation visibly relies on sequence storage, work queue.
- No explicit loop blocks detected.
Complexity
Count heap pushes and pops; each normally contributes a logarithmic factor in the heap size.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution:2 def getFinalState(3 self,4 nums: list[int],5 k: int,6 multiplier: int7 ) -> list[int]:8 if multiplier == 1:9 return nums10 11 MOD = 1_000_000_00712 n = len(nums)13 maxNum = max(nums)14 ans = [0] * n15 minHeap = [(num, i) for i, num in enumerate(nums)]16 17 heapq.heapify(minHeap)18 19 20 21 22 23 while k > 0 and minHeap[0][0] * multiplier <= maxNum:24 num, i = heapq.heappop(minHeap)25 heapq.heappush(minHeap, (num * multiplier, i))26 k -= 127 28 sortedIndexedNums = sorted(minHeap)29 multipliesPerNum, remainingK = divmod(k, n)30 31 32 33 for index, (num, i) in enumerate(sortedIndexedNums):34 sortedIndexedNums[index] = (35 sortedIndexedNums[index][0] *36 pow(multiplier, multipliesPerNum, MOD) % MOD, i)37 38 39 40 for index in range(remainingK):41 sortedIndexedNums[index] = (42 sortedIndexedNums[index][0] * multiplier % MOD,43 sortedIndexedNums[index][1])44 45 for num, i in sortedIndexedNums:46 ans[i] = num47 48 return ans49