Final Array State After K Multiplication Operations I
Apply k multiplications — each picks the smallest element (smallest index on ties) and multiplies it by m.
5 min read
top-k heaps
Problem#
Each operation finds the smallest element (lowest index on ties) and multiplies it by multiplier. After k operations, return the array.
Examples#
nums = [2,1,3,5,6], k = 5, multiplier = 2→[8,4,6,5,6]
Constraints#
1 <= n <= 100,1 <= k <= 10,1 <= multiplier <= 5.
Approach#
Min-heap of (value, index). Pop, multiply, push.
Solution#
class Solution {public: vector<int> getFinalState(vector<int>& nums, int k, int multiplier) { priority_queue<pair<int,int>, vector<pair<int,int>>, greater<>> pq; for (int i = 0; i < (int)nums.size(); ++i) pq.push({nums[i], i}); while (k--) { auto [v, i] = pq.top(); pq.pop(); nums[i] = v * multiplier; pq.push({nums[i], i}); } return nums; }};import "container/heap"
type item struct{ val, idx int }type minHeap []item
func (h minHeap) Len() int { return len(h) }func (h minHeap) Less(i, j int) bool { if h[i].val != h[j].val { return h[i].val < h[j].val } return h[i].idx < h[j].idx}func (h minHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }func (h *minHeap) Push(x interface{}) { *h = append(*h, x.(item)) }func (h *minHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[:n-1] return x}
func getFinalState(nums []int, k int, multiplier int) []int { h := &minHeap{} for i, v := range nums { heap.Push(h, item{v, i}) } for k > 0 { x := heap.Pop(h).(item) nums[x.idx] = x.val * multiplier heap.Push(h, item{nums[x.idx], x.idx}) k-- } return nums}import heapqfrom typing import List
class Solution: def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]: heap = [(v, i) for i, v in enumerate(nums)] heapq.heapify(heap) for _ in range(k): v, i = heapq.heappop(heap) nums[i] = v * multiplier heapq.heappush(heap, (nums[i], i)) return numsclass MinHeap { constructor() { this.h = []; } size() { return this.h.length; } push(x) { this.h.push(x); let i = this.h.length - 1; while (i > 0) { const p = (i - 1) >> 1; if (this._less(i, p)) { [this.h[i], this.h[p]] = [this.h[p], this.h[i]]; i = p; } else break; } } pop() { const top = this.h[0]; const last = this.h.pop(); if (this.h.length > 0) { this.h[0] = last; let i = 0; const n = this.h.length; while (true) { const l = i * 2 + 1, r = i * 2 + 2; let best = i; if (l < n && this._less(l, best)) best = l; if (r < n && this._less(r, best)) best = r; if (best === i) break; [this.h[i], this.h[best]] = [this.h[best], this.h[i]]; i = best; } } return top; } _less(i, j) { const a = this.h[i], b = this.h[j]; if (a[0] !== b[0]) return a[0] < b[0]; return a[1] < b[1]; }}
function getFinalState(nums, k, multiplier) { const heap = new MinHeap(); for (let i = 0; i < nums.length; i++) heap.push([nums[i], i]); for (let op = 0; op < k; op++) { const [v, i] = heap.pop(); nums[i] = v * multiplier; heap.push([nums[i], i]); } return nums;}class Solution { public int[] getFinalState(int[] nums, int k, int multiplier) { PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]); for (int i = 0; i < nums.length; i++) pq.offer(new int[]{nums[i], i}); while (k-- > 0) { int[] top = pq.poll(); nums[top[1]] = top[0] * multiplier; pq.offer(new int[]{nums[top[1]], top[1]}); } return nums; }}class MinHeap { private h: Array<[number, number]> = []; size(): number { return this.h.length; } push(x: [number, number]): void { this.h.push(x); let i = this.h.length - 1; while (i > 0) { const p = (i - 1) >> 1; if (this.less(i, p)) { [this.h[i], this.h[p]] = [this.h[p], this.h[i]]; i = p; } else break; } } pop(): [number, number] { const top = this.h[0]; const last = this.h.pop() as [number, number]; if (this.h.length > 0) { this.h[0] = last; let i = 0; const n = this.h.length; while (true) { const l = i * 2 + 1, r = i * 2 + 2; let best = i; if (l < n && this.less(l, best)) best = l; if (r < n && this.less(r, best)) best = r; if (best === i) break; [this.h[i], this.h[best]] = [this.h[best], this.h[i]]; i = best; } } return top; } private less(i: number, j: number): boolean { const a = this.h[i], b = this.h[j]; if (a[0] !== b[0]) return a[0] < b[0]; return a[1] < b[1]; }}
function getFinalState(nums: number[], k: number, multiplier: number): number[] { const heap = new MinHeap(); for (let i = 0; i < nums.length; i++) heap.push([nums[i], i]); for (let op = 0; op < k; op++) { const [v, i] = heap.pop(); nums[i] = v * multiplier; heap.push([nums[i], i]); } return nums;}Editorial#
Tuple (value, index) makes the heap order break ties by index automatically (smaller index has higher priority). Each operation is one pop + one push.
Complexity#
- Time: O((n + k) log n).
- Space: O(n).
Concept revision#
Related#