2968. Apply Operations to Maximize Frequency Score Hard
1/**
2 * [2968] Apply Operations to Maximize Frequency Score
3 *
4 * You are given a 0-indexed integer array nums and an integer k.
5 * You can perform the following operation on the array at most k times:
6 *
7 * Choose any index i from the array and increase or decrease nums[i] by 1.
8 *
9 * The score of the final array is the frequency of the most frequent element in the array.
10 * Return the maximum score you can achieve.
11 * The frequency of an element is the number of occurences of that element in the array.
12 *
13 * <strong class="example">Example 1:
14 *
15 * Input: nums = [1,2,6,4], k = 3
16 * Output: 3
17 * Explanation: We can do the following operations on the array:
18 * - Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4].
19 * - Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3].
20 * - Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2].
21 * The element 2 is the most frequent in the final array so our score is 3.
22 * It can be shown that we cannot achieve a better score.
23 *
24 * <strong class="example">Example 2:
25 *
26 * Input: nums = [1,4,4,2,4], k = 0
27 * Output: 3
28 * Explanation: We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3.
29 *
30 *
31 * Constraints:
32 *
33 * 1 <= nums.length <= 10^5
34 * 1 <= nums[i] <= 10^9
35 * 0 <= k <= 10^14
36 *
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/apply-operations-to-maximize-frequency-score/
41// discuss: https://leetcode.com/problems/apply-operations-to-maximize-frequency-score/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46 pub fn max_frequency_score(nums: Vec<i32>, k: i64) -> i32 {
47 0
48 }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn test_2968() {
59 }
60}
61
Back
© 2025 bowen.ge All Rights Reserved.