Space
Open
From Fieldwork
Scales
Archive
It's about sliding windows.
class Solution:
def maxFrequency(self, nums, k):
nums.sort()
left = 0
window_sum = 0
best = 1
for right, target in enumerate(nums):
window_sum += target
# cost to raise nums[left..right] up to nums[right] (target)
# = target * window_size - window_sum
while target * (right - left + 1) - window_sum > k:
window_sum -= nums[left]
left += 1
best = max(best, right - left + 1)
return bestPractice bench
A private scratchpad for this reading. Nothing is sent or scored.
What is still unclear, or what would change the explanation?
Saved on this device · one draft per mode