Frequency of the Most Frequent Element LeetCode solution in JavaScript

1
var maxFrequency = function (nums, k) {
2
// We sorted because as we are allowed only to increment the value & we try to increase the smaller el to some larger el
3
nums.sort((a, b) => a - b);
4

5
let left = 0;
6
let max = Math.max(); // without any args, Math.max() is -Infinity
7
let curr = 0;
8
// I have used 'for loop' so rightPtr is 'i' here
9
for (let i = 0; i < nums.length; i++) {
10
curr += nums[i];
11
// decrement the winSize once the value required to convert could not be achieved even after utilizing K
12
// (i-left+1) * nums[i] because we are converting every el upto that winSize by current el
13
while ((i - left + 1) * nums[i] > curr + k) {
14
curr -= nums[left++];
15
}
16
max = Math.max(max, i - left + 1);
17
}
18
return max;
19
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0