Source Code: class Solution: def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]: ''' To solve this problem, we essentially have to go through each element in the array, find the smallest number that we've seen, and we multiply it with the given multiplier. We repeat this k times. I first thought about perhaps using a priority queue to extract the smallest element easily, but I realized that this could change the initial order of our array, in which we kind of need to preserve our original order. Going through the array n times isn't too bad for our time complexity, so I figured that the brute force solution would be the best here :) ''' smallest = float('inf') chosen_one = 0 for i in range(k): for j in range(len(nums)): if nums[j] < smallest: chosen_one = j smallest = nums[j] nums[chosen_one] *= multiplier smallest = float('inf') return nums
Source Code:
class Solution:
def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
'''
To solve this problem, we essentially have to go through each element
in the array, find the smallest number that we've seen, and we
multiply it with the given multiplier. We repeat this k times. I first
thought about perhaps using a priority queue to extract the smallest
element easily, but I realized that this could change the initial
order of our array, in which we kind of need to preserve our
original order. Going through the array n times isn't too bad for
our time complexity, so I figured that the brute force solution
would be the best here :)
'''
smallest = float('inf')
chosen_one = 0
for i in range(k):
for j in range(len(nums)):
if nums[j] < smallest:
chosen_one = j
smallest = nums[j]
nums[chosen_one] *= multiplier
smallest = float('inf')
return nums