Skip to content

Commit 7ffcdf6

Browse files
authored
feat: add solutions to lc problem: No.3347 (#4795)
1 parent 0ac3965 commit 7ffcdf6

File tree

4 files changed

+203
-0
lines changed

4 files changed

+203
-0
lines changed

solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,77 @@ function maxFrequency(nums: number[], k: number, numOperations: number): number
237237
}
238238
```
239239

240+
#### Rust
241+
242+
```rust
243+
use std::collections::{HashMap, BTreeMap};
244+
245+
impl Solution {
246+
pub fn max_frequency(nums: Vec<i32>, k: i32, num_operations: i32) -> i32 {
247+
let mut cnt = HashMap::new();
248+
let mut d = BTreeMap::new();
249+
250+
for &x in &nums {
251+
*cnt.entry(x).or_insert(0) += 1;
252+
d.entry(x).or_insert(0);
253+
*d.entry(x - k).or_insert(0) += 1;
254+
*d.entry(x + k + 1).or_insert(0) -= 1;
255+
}
256+
257+
let mut ans = 0;
258+
let mut s = 0;
259+
for (&x, &t) in d.iter() {
260+
s += t;
261+
let cur = s.min(cnt.get(&x).copied().unwrap_or(0) + num_operations);
262+
ans = ans.max(cur);
263+
}
264+
265+
ans
266+
}
267+
}
268+
```
269+
270+
#### C#
271+
272+
```cs
273+
public class Solution {
274+
public int MaxFrequency(int[] nums, int k, int numOperations) {
275+
var cnt = new Dictionary<int, int>();
276+
var d = new SortedDictionary<int, int>();
277+
278+
foreach (var x in nums) {
279+
if (!cnt.ContainsKey(x)) {
280+
cnt[x] = 0;
281+
}
282+
cnt[x]++;
283+
284+
if (!d.ContainsKey(x)) {
285+
d[x] = 0;
286+
}
287+
if (!d.ContainsKey(x - k)) {
288+
d[x - k] = 0;
289+
}
290+
if (!d.ContainsKey(x + k + 1)) {
291+
d[x + k + 1] = 0;
292+
}
293+
294+
d[x - k] += 1;
295+
d[x + k + 1] -= 1;
296+
}
297+
298+
int ans = 0, s = 0;
299+
foreach (var kvp in d) {
300+
int x = kvp.Key, t = kvp.Value;
301+
s += t;
302+
int cur = Math.Min(s, (cnt.ContainsKey(x) ? cnt[x] : 0) + numOperations);
303+
ans = Math.Max(ans, cur);
304+
}
305+
306+
return ans;
307+
}
308+
}
309+
```
310+
240311
<!-- tabs:end -->
241312

242313
<!-- solution:end -->

solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/README_EN.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,77 @@ function maxFrequency(nums: number[], k: number, numOperations: number): number
233233
}
234234
```
235235

236+
#### Rust
237+
238+
```rust
239+
use std::collections::{HashMap, BTreeMap};
240+
241+
impl Solution {
242+
pub fn max_frequency(nums: Vec<i32>, k: i32, num_operations: i32) -> i32 {
243+
let mut cnt = HashMap::new();
244+
let mut d = BTreeMap::new();
245+
246+
for &x in &nums {
247+
*cnt.entry(x).or_insert(0) += 1;
248+
d.entry(x).or_insert(0);
249+
*d.entry(x - k).or_insert(0) += 1;
250+
*d.entry(x + k + 1).or_insert(0) -= 1;
251+
}
252+
253+
let mut ans = 0;
254+
let mut s = 0;
255+
for (&x, &t) in d.iter() {
256+
s += t;
257+
let cur = s.min(cnt.get(&x).copied().unwrap_or(0) + num_operations);
258+
ans = ans.max(cur);
259+
}
260+
261+
ans
262+
}
263+
}
264+
```
265+
266+
#### C#
267+
268+
```cs
269+
public class Solution {
270+
public int MaxFrequency(int[] nums, int k, int numOperations) {
271+
var cnt = new Dictionary<int, int>();
272+
var d = new SortedDictionary<int, int>();
273+
274+
foreach (var x in nums) {
275+
if (!cnt.ContainsKey(x)) {
276+
cnt[x] = 0;
277+
}
278+
cnt[x]++;
279+
280+
if (!d.ContainsKey(x)) {
281+
d[x] = 0;
282+
}
283+
if (!d.ContainsKey(x - k)) {
284+
d[x - k] = 0;
285+
}
286+
if (!d.ContainsKey(x + k + 1)) {
287+
d[x + k + 1] = 0;
288+
}
289+
290+
d[x - k] += 1;
291+
d[x + k + 1] -= 1;
292+
}
293+
294+
int ans = 0, s = 0;
295+
foreach (var kvp in d) {
296+
int x = kvp.Key, t = kvp.Value;
297+
s += t;
298+
int cur = Math.Min(s, (cnt.ContainsKey(x) ? cnt[x] : 0) + numOperations);
299+
ans = Math.Max(ans, cur);
300+
}
301+
302+
return ans;
303+
}
304+
}
305+
```
306+
236307
<!-- tabs:end -->
237308

238309
<!-- solution:end -->
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class Solution {
2+
public int MaxFrequency(int[] nums, int k, int numOperations) {
3+
var cnt = new Dictionary<int, int>();
4+
var d = new SortedDictionary<int, int>();
5+
6+
foreach (var x in nums) {
7+
if (!cnt.ContainsKey(x)) {
8+
cnt[x] = 0;
9+
}
10+
cnt[x]++;
11+
12+
if (!d.ContainsKey(x)) {
13+
d[x] = 0;
14+
}
15+
if (!d.ContainsKey(x - k)) {
16+
d[x - k] = 0;
17+
}
18+
if (!d.ContainsKey(x + k + 1)) {
19+
d[x + k + 1] = 0;
20+
}
21+
22+
d[x - k] += 1;
23+
d[x + k + 1] -= 1;
24+
}
25+
26+
int ans = 0, s = 0;
27+
foreach (var kvp in d) {
28+
int x = kvp.Key, t = kvp.Value;
29+
s += t;
30+
int cur = Math.Min(s, (cnt.ContainsKey(x) ? cnt[x] : 0) + numOperations);
31+
ans = Math.Max(ans, cur);
32+
}
33+
34+
return ans;
35+
}
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use std::collections::{HashMap, BTreeMap};
2+
3+
impl Solution {
4+
pub fn max_frequency(nums: Vec<i32>, k: i32, num_operations: i32) -> i32 {
5+
let mut cnt = HashMap::new();
6+
let mut d = BTreeMap::new();
7+
8+
for &x in &nums {
9+
*cnt.entry(x).or_insert(0) += 1;
10+
d.entry(x).or_insert(0);
11+
*d.entry(x - k).or_insert(0) += 1;
12+
*d.entry(x + k + 1).or_insert(0) -= 1;
13+
}
14+
15+
let mut ans = 0;
16+
let mut s = 0;
17+
for (&x, &t) in d.iter() {
18+
s += t;
19+
let cur = s.min(cnt.get(&x).copied().unwrap_or(0) + num_operations);
20+
ans = ans.max(cur);
21+
}
22+
23+
ans
24+
}
25+
}

0 commit comments

Comments
 (0)