3347. Maximum Frequency of an Element After Performing Operations II Hard
1/**
2 * [3347] Maximum Frequency of an Element After Performing Operations II
3 *
4 * You are given an integer array nums and two integers k and numOperations.
5 * You must perform an operation numOperations times on nums, where in each operation you:
6 *
7 * Select an index i that was not selected in any previous operations.
8 * Add an integer in the range [-k, k] to nums[i].
9 *
10 * Return the maximum possible <span data-keyword="frequency-array">frequency</span> of any element in nums after performing the operations.
11 *
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">nums = [1,4,5], k = 1, numOperations = 2</span>
15 * Output: <span class="example-io">2</span>
16 * Explanation:
17 * We can achieve a maximum frequency of two by:
18 *
19 * Adding 0 to nums[1], after which nums becomes [1, 4, 5].
20 * Adding -1 to nums[2], after which nums becomes [1, 4, 4].
21 * </div>
22 * <strong class="example">Example 2:
23 * <div class="example-block">
24 * Input: <span class="example-io">nums = [5,11,20,20], k = 5, numOperations = 1</span>
25 * Output: <span class="example-io">2</span>
26 * Explanation:
27 * We can achieve a maximum frequency of two by:
28 *
29 * Adding 0 to nums[1].
30 * </div>
31 *
32 * Constraints:
33 *
34 * 1 <= nums.length <= 10^5
35 * 1 <= nums[i] <= 10^9
36 * 0 <= k <= 10^9
37 * 0 <= numOperations <= nums.length
38 *
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/maximum-frequency-of-an-element-after-performing-operations-ii/
43// discuss: https://leetcode.com/problems/maximum-frequency-of-an-element-after-performing-operations-ii/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48 pub fn max_frequency(nums: Vec<i32>, k: i32, num_operations: i32) -> i32 {
49 0
50 }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_3347() {
61 }
62}
63
Back
© 2025 bowen.ge All Rights Reserved.