1838. Frequency of the Most Frequent Element Medium
1/**
2 * [1838] Frequency of the Most Frequent Element
3 *
4 * The frequency of an element is the number of times it occurs in an array.
5 * You are given an integer array nums and an integer k. In one operation, you can choose an index of nums and increment the element at that index by 1.
6 * Return the maximum possible frequency of an element after performing at most k operations.
7 *
8 * Example 1:
9 *
10 * Input: nums = [1,2,4], k = 5
11 * Output: 3
12 * Explanation: Increment the first element three times and the second element two times to make nums = [4,4,4].
13 * 4 has a frequency of 3.
14 * Example 2:
15 *
16 * Input: nums = [1,4,8,13], k = 5
17 * Output: 2
18 * Explanation: There are multiple optimal solutions:
19 * - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2.
20 * - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2.
21 * - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.
22 *
23 * Example 3:
24 *
25 * Input: nums = [3,9,6], k = 2
26 * Output: 1
27 *
28 *
29 * Constraints:
30 *
31 * 1 <= nums.length <= 10^5
32 * 1 <= nums[i] <= 10^5
33 * 1 <= k <= 10^5
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/frequency-of-the-most-frequent-element/
39// discuss: https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn max_frequency(nums: Vec<i32>, k: i32) -> i32 {
45 0
46 }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_1838() {
57 }
58}
59
Back
© 2025 bowen.ge All Rights Reserved.