Skip to content

Commit 497156e

Browse files
committed
Create: 1838-frequency-of-the-most-frequent-element.rs / .go
1 parent ee7a3bc commit 497156e

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import (
2+
"math"
3+
"sort"
4+
)
5+
6+
func maxFrequency(nums []int, k int) int {
7+
sort.Ints(nums)
8+
9+
left, right, res, total := 0,0,0,0
10+
11+
for right < len(nums) {
12+
total += nums[right]
13+
14+
for nums[right] * (right - left +1) > total + k {
15+
total -= nums[left]
16+
left++
17+
}
18+
19+
res = int(math.Max(float64(res), float64(right - left + 1)))
20+
right++
21+
}
22+
23+
return res
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn max_frequency(mut nums: Vec<i32>, k: i32) -> i32 {
3+
let (mut left, mut right, mut res, mut total) = (0, 0, 0, 0);
4+
5+
nums.sort_unstable();
6+
7+
while right < nums.len() {
8+
total += nums[right];
9+
10+
while (nums[right] * (right - left + 1) as i32) - total > k {
11+
total -= nums[left];
12+
left += 1;
13+
}
14+
15+
res = res.max(right - left + 1);
16+
right += 1;
17+
}
18+
19+
res as i32
20+
}
21+
}

0 commit comments

Comments
 (0)