From 9887411c49da1b165de254b1fabfb5ac1a8c3571 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 22 Oct 2025 07:14:36 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3347 --- .../README.md | 71 +++++++++++++++++++ .../README_EN.md | 71 +++++++++++++++++++ .../Solution.cs | 36 ++++++++++ .../Solution.rs | 25 +++++++ 4 files changed, 203 insertions(+) create mode 100644 solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/Solution.cs create mode 100644 solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/Solution.rs diff --git a/solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/README.md b/solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/README.md index 100b915f8a110..52a36c63c66bb 100644 --- a/solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/README.md +++ b/solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/README.md @@ -237,6 +237,77 @@ function maxFrequency(nums: number[], k: number, numOperations: number): number } ``` +#### Rust + +```rust +use std::collections::{HashMap, BTreeMap}; + +impl Solution { + pub fn max_frequency(nums: Vec, k: i32, num_operations: i32) -> i32 { + let mut cnt = HashMap::new(); + let mut d = BTreeMap::new(); + + for &x in &nums { + *cnt.entry(x).or_insert(0) += 1; + d.entry(x).or_insert(0); + *d.entry(x - k).or_insert(0) += 1; + *d.entry(x + k + 1).or_insert(0) -= 1; + } + + let mut ans = 0; + let mut s = 0; + for (&x, &t) in d.iter() { + s += t; + let cur = s.min(cnt.get(&x).copied().unwrap_or(0) + num_operations); + ans = ans.max(cur); + } + + ans + } +} +``` + +#### C# + +```cs +public class Solution { + public int MaxFrequency(int[] nums, int k, int numOperations) { + var cnt = new Dictionary(); + var d = new SortedDictionary(); + + foreach (var x in nums) { + if (!cnt.ContainsKey(x)) { + cnt[x] = 0; + } + cnt[x]++; + + if (!d.ContainsKey(x)) { + d[x] = 0; + } + if (!d.ContainsKey(x - k)) { + d[x - k] = 0; + } + if (!d.ContainsKey(x + k + 1)) { + d[x + k + 1] = 0; + } + + d[x - k] += 1; + d[x + k + 1] -= 1; + } + + int ans = 0, s = 0; + foreach (var kvp in d) { + int x = kvp.Key, t = kvp.Value; + s += t; + int cur = Math.Min(s, (cnt.ContainsKey(x) ? cnt[x] : 0) + numOperations); + ans = Math.Max(ans, cur); + } + + return ans; + } +} +``` + diff --git a/solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/README_EN.md b/solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/README_EN.md index f98a49f6fc9ea..f997eb52c06ea 100644 --- a/solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/README_EN.md +++ b/solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/README_EN.md @@ -233,6 +233,77 @@ function maxFrequency(nums: number[], k: number, numOperations: number): number } ``` +#### Rust + +```rust +use std::collections::{HashMap, BTreeMap}; + +impl Solution { + pub fn max_frequency(nums: Vec, k: i32, num_operations: i32) -> i32 { + let mut cnt = HashMap::new(); + let mut d = BTreeMap::new(); + + for &x in &nums { + *cnt.entry(x).or_insert(0) += 1; + d.entry(x).or_insert(0); + *d.entry(x - k).or_insert(0) += 1; + *d.entry(x + k + 1).or_insert(0) -= 1; + } + + let mut ans = 0; + let mut s = 0; + for (&x, &t) in d.iter() { + s += t; + let cur = s.min(cnt.get(&x).copied().unwrap_or(0) + num_operations); + ans = ans.max(cur); + } + + ans + } +} +``` + +#### C# + +```cs +public class Solution { + public int MaxFrequency(int[] nums, int k, int numOperations) { + var cnt = new Dictionary(); + var d = new SortedDictionary(); + + foreach (var x in nums) { + if (!cnt.ContainsKey(x)) { + cnt[x] = 0; + } + cnt[x]++; + + if (!d.ContainsKey(x)) { + d[x] = 0; + } + if (!d.ContainsKey(x - k)) { + d[x - k] = 0; + } + if (!d.ContainsKey(x + k + 1)) { + d[x + k + 1] = 0; + } + + d[x - k] += 1; + d[x + k + 1] -= 1; + } + + int ans = 0, s = 0; + foreach (var kvp in d) { + int x = kvp.Key, t = kvp.Value; + s += t; + int cur = Math.Min(s, (cnt.ContainsKey(x) ? cnt[x] : 0) + numOperations); + ans = Math.Max(ans, cur); + } + + return ans; + } +} +``` + diff --git a/solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/Solution.cs b/solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/Solution.cs new file mode 100644 index 0000000000000..3a7b4c4e1917b --- /dev/null +++ b/solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/Solution.cs @@ -0,0 +1,36 @@ +public class Solution { + public int MaxFrequency(int[] nums, int k, int numOperations) { + var cnt = new Dictionary(); + var d = new SortedDictionary(); + + foreach (var x in nums) { + if (!cnt.ContainsKey(x)) { + cnt[x] = 0; + } + cnt[x]++; + + if (!d.ContainsKey(x)) { + d[x] = 0; + } + if (!d.ContainsKey(x - k)) { + d[x - k] = 0; + } + if (!d.ContainsKey(x + k + 1)) { + d[x + k + 1] = 0; + } + + d[x - k] += 1; + d[x + k + 1] -= 1; + } + + int ans = 0, s = 0; + foreach (var kvp in d) { + int x = kvp.Key, t = kvp.Value; + s += t; + int cur = Math.Min(s, (cnt.ContainsKey(x) ? cnt[x] : 0) + numOperations); + ans = Math.Max(ans, cur); + } + + return ans; + } +} diff --git a/solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/Solution.rs b/solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/Solution.rs new file mode 100644 index 0000000000000..6b3914c4a55ad --- /dev/null +++ b/solution/3300-3399/3347.Maximum Frequency of an Element After Performing Operations II/Solution.rs @@ -0,0 +1,25 @@ +use std::collections::{HashMap, BTreeMap}; + +impl Solution { + pub fn max_frequency(nums: Vec, k: i32, num_operations: i32) -> i32 { + let mut cnt = HashMap::new(); + let mut d = BTreeMap::new(); + + for &x in &nums { + *cnt.entry(x).or_insert(0) += 1; + d.entry(x).or_insert(0); + *d.entry(x - k).or_insert(0) += 1; + *d.entry(x + k + 1).or_insert(0) -= 1; + } + + let mut ans = 0; + let mut s = 0; + for (&x, &t) in d.iter() { + s += t; + let cur = s.min(cnt.get(&x).copied().unwrap_or(0) + num_operations); + ans = ans.max(cur); + } + + ans + } +}