Skip to content

Commit 0913b2b

Browse files
committed
feat: add ts solution to lc problem: No.1838
No.1838.Frequency of the Most Frequent Element
1 parent c453875 commit 0913b2b

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

solution/1800-1899/1838.Frequency of the Most Frequent Element/README.md

+52
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272

7373
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
7474

75+
<!-- tabs:start -->
76+
7577
### **Python3**
7678

7779
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -348,6 +350,56 @@ var maxFrequency = function (nums, k) {
348350
};
349351
```
350352

353+
### **TypeScript**
354+
355+
```ts
356+
function maxFrequency(nums: number[], k: number): number {
357+
nums.sort((a, b) => a - b);
358+
let ans = 1;
359+
let window = 0;
360+
const n = nums.length;
361+
for (let l = 0, r = 1; r < n; ++r) {
362+
window += (nums[r] - nums[r - 1]) * (r - l);
363+
while (window > k) {
364+
window -= nums[r] - nums[l++];
365+
}
366+
ans = Math.max(ans, r - l + 1);
367+
}
368+
return ans;
369+
}
370+
```
371+
372+
```ts
373+
function maxFrequency(nums: number[], k: number): number {
374+
nums.sort((a, b) => a - b);
375+
const n = nums.length;
376+
const s = new Array(n + 1).fill(0);
377+
for (let i = 0; i < n; ++i) {
378+
s[i + 1] = s[i] + nums[i];
379+
}
380+
const check = (cnt: number) => {
381+
for (let i = 0; i < n + 1 - cnt; ++i) {
382+
const j = i + cnt - 1;
383+
if (nums[j] * cnt - (s[j + 1] - s[i]) <= k) {
384+
return true;
385+
}
386+
}
387+
return false;
388+
};
389+
let left = 1;
390+
let right = n;
391+
while (left < right) {
392+
const mid = (left + right + 1) >> 1;
393+
if (check(mid)) {
394+
left = mid;
395+
} else {
396+
right = mid - 1;
397+
}
398+
}
399+
return left;
400+
}
401+
```
402+
351403
### **...**
352404

353405
```

solution/1800-1899/1838.Frequency of the Most Frequent Element/README_EN.md

+50
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,56 @@ var maxFrequency = function (nums, k) {
322322
};
323323
```
324324

325+
### **TypeScript**
326+
327+
```ts
328+
function maxFrequency(nums: number[], k: number): number {
329+
nums.sort((a, b) => a - b);
330+
let ans = 1;
331+
let window = 0;
332+
const n = nums.length;
333+
for (let l = 0, r = 1; r < n; ++r) {
334+
window += (nums[r] - nums[r - 1]) * (r - l);
335+
while (window > k) {
336+
window -= nums[r] - nums[l++];
337+
}
338+
ans = Math.max(ans, r - l + 1);
339+
}
340+
return ans;
341+
}
342+
```
343+
344+
```ts
345+
function maxFrequency(nums: number[], k: number): number {
346+
nums.sort((a, b) => a - b);
347+
const n = nums.length;
348+
const s = new Array(n + 1).fill(0);
349+
for (let i = 0; i < n; ++i) {
350+
s[i + 1] = s[i] + nums[i];
351+
}
352+
const check = (cnt: number) => {
353+
for (let i = 0; i < n + 1 - cnt; ++i) {
354+
const j = i + cnt - 1;
355+
if (nums[j] * cnt - (s[j + 1] - s[i]) <= k) {
356+
return true;
357+
}
358+
}
359+
return false;
360+
};
361+
let left = 1;
362+
let right = n;
363+
while (left < right) {
364+
const mid = (left + right + 1) >> 1;
365+
if (check(mid)) {
366+
left = mid;
367+
} else {
368+
right = mid - 1;
369+
}
370+
}
371+
return left;
372+
}
373+
```
374+
325375
### **...**
326376

327377
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function maxFrequency(nums: number[], k: number): number {
2+
nums.sort((a, b) => a - b);
3+
let ans = 1;
4+
let window = 0;
5+
const n = nums.length;
6+
for (let l = 0, r = 1; r < n; ++r) {
7+
window += (nums[r] - nums[r - 1]) * (r - l);
8+
while (window > k) {
9+
window -= nums[r] - nums[l++];
10+
}
11+
ans = Math.max(ans, r - l + 1);
12+
}
13+
return ans;
14+
}

0 commit comments

Comments
 (0)