Back to course home
0% completed
Vote For New Content
Alternative solution with O(N) time complexity and O(nums range) space complexity
Matteo Mortella
Mar 8, 2025
def reductionOperations(self, nums): #time O(N), space #O(nums range) nums_max = max(nums) # time O(N), space O(1) nums_min = min(nums) # time O(N), space O(1) freq_arr = [0] * (nums_max - nums_min) #space O(nums range) for num in nums: #O(N) if num != nums_min: freq_arr[abs(num - nums_max)] += 1 accumulator = 0 n_operations = 0 for i in range(len(freq_arr)): #O(N) if freq_arr[i] > 0: freq_arr[i] += accumulator accumulator = freq_arr[i] n_operations += freq_arr[i] return n_operations
0
0
Comments
Comments
On this page