Welcome to Subscribe On Youtube
3347. Maximum Frequency of an Element After Performing Operations II
Description
You are given an integer array nums and two integers k and numOperations.
You must perform an operation numOperations times on nums, where in each operation you:
- Select an index
ithat was not selected in any previous operations. - Add an integer in the range
[-k, k]tonums[i].
Return the maximum possible frequency of any element in nums after performing the operations.
Example 1:
Input: nums = [1,4,5], k = 1, numOperations = 2
Output: 2
Explanation:
We can achieve a maximum frequency of two by:
- Adding 0 to
nums[1], after whichnumsbecomes[1, 4, 5]. - Adding -1 to
nums[2], after whichnumsbecomes[1, 4, 4].
Example 2:
Input: nums = [5,11,20,20], k = 5, numOperations = 1
Output: 2
Explanation:
We can achieve a maximum frequency of two by:
- Adding 0 to
nums[1].
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1090 <= k <= 1090 <= numOperations <= nums.length
Solutions
Solution 1
-
class Solution { public int maxFrequency(int[] nums, int k, int numOperations) { Map<Integer, Integer> cnt = new HashMap<>(); TreeMap<Integer, Integer> d = new TreeMap<>(); for (int x : nums) { cnt.merge(x, 1, Integer::sum); d.putIfAbsent(x, 0); d.merge(x - k, 1, Integer::sum); d.merge(x + k + 1, -1, Integer::sum); } int ans = 0, s = 0; for (var e : d.entrySet()) { int x = e.getKey(), t = e.getValue(); s += t; ans = Math.max(ans, Math.min(s, cnt.getOrDefault(x, 0) + numOperations)); } return ans; } } -
class Solution { public: int maxFrequency(vector<int>& nums, int k, int numOperations) { unordered_map<int, int> cnt; map<int, int> d; for (int x : nums) { cnt[x]++; d[x]; d[x - k]++; d[x + k + 1]--; } int ans = 0, s = 0; for (const auto& [x, t] : d) { s += t; ans = max(ans, min(s, cnt[x] + numOperations)); } return ans; } }; -
class Solution: def maxFrequency(self, nums: List[int], k: int, numOperations: int) -> int: cnt = defaultdict(int) d = defaultdict(int) for x in nums: cnt[x] += 1 d[x] += 0 d[x - k] += 1 d[x + k + 1] -= 1 ans = s = 0 for x, t in sorted(d.items()): s += t ans = max(ans, min(s, cnt[x] + numOperations)) return ans -
func maxFrequency(nums []int, k int, numOperations int) (ans int) { cnt := make(map[int]int) d := make(map[int]int) for _, x := range nums { cnt[x]++ d[x] = d[x] d[x-k]++ d[x+k+1]-- } s := 0 keys := make([]int, 0, len(d)) for key := range d { keys = append(keys, key) } sort.Ints(keys) for _, x := range keys { s += d[x] ans = max(ans, min(s, cnt[x]+numOperations)) } return } -
function maxFrequency(nums: number[], k: number, numOperations: number): number { const cnt: Record<number, number> = {}; const d: Record<number, number> = {}; for (const x of nums) { cnt[x] = (cnt[x] || 0) + 1; d[x] = d[x] || 0; d[x - k] = (d[x - k] || 0) + 1; d[x + k + 1] = (d[x + k + 1] || 0) - 1; } let [ans, s] = [0, 0]; const keys = Object.keys(d) .map(Number) .sort((a, b) => a - b); for (const x of keys) { s += d[x]; ans = Math.max(ans, Math.min(s, (cnt[x] || 0) + numOperations)); } return ans; } -
public class Solution { public int MaxFrequency(int[] nums, int k, int numOperations) { var cnt = new Dictionary<int, int>(); var d = new SortedDictionary<int, int>(); 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; } } -
use std::collections::{HashMap, BTreeMap}; impl Solution { pub fn max_frequency(nums: Vec<i32>, 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 } }