Skip to content

Commit

Permalink
1838-frequency-of-the-most-frequent-element.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
andynullwong committed Jan 2, 2023
1 parent 9651ded commit fdb2771
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions typescript/1838-frequency-of-the-most-frequent-element.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function maxFrequency(nums: number[], k: number): number {
const sortedNums: number[] = nums.sort((a: number, b: number) => a - b);

let maxLength: number = 0;

let currentSum: number = 0;
let leftWindow: number = 0;
for (let rightWindow: number = 0; rightWindow < sortedNums.length; rightWindow++) {
const currentLength: number = rightWindow - leftWindow + 1;
const rightNum: number = sortedNums[rightWindow];
currentSum += rightNum;

if (currentSum + k >= rightNum * currentLength) {
maxLength = currentLength;
} else {
const leftNum = sortedNums[leftWindow];
currentSum -= leftNum;
leftWindow++;
}
}
return maxLength;
};

0 comments on commit fdb2771

Please sign in to comment.