Skip to content

Commit 402b84f

Browse files
committed
feat: finish 1838
1 parent e877c16 commit 402b84f

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# 1838. Frequency of the Most Frequent Element
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Binary Search, Greedy, Sliding Window, Sorting, Prefix Sum.
5+
- Similar Questions: Find All Lonely Numbers in the Array, Longest Nice Subarray.
6+
7+
## Problem
8+
9+
The **frequency** of an element is the number of times it occurs in an array.
10+
11+
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```.
12+
13+
Return **the **maximum possible frequency** of an element after performing **at most** **```k```** operations**.
14+
15+
 
16+
Example 1:
17+
18+
```
19+
Input: nums = [1,2,4], k = 5
20+
Output: 3
21+
Explanation: Increment the first element three times and the second element two times to make nums = [4,4,4].
22+
4 has a frequency of 3.
23+
```
24+
25+
Example 2:
26+
27+
```
28+
Input: nums = [1,4,8,13], k = 5
29+
Output: 2
30+
Explanation: There are multiple optimal solutions:
31+
- Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2.
32+
- Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2.
33+
- Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.
34+
```
35+
36+
Example 3:
37+
38+
```
39+
Input: nums = [3,9,6], k = 2
40+
Output: 1
41+
```
42+
43+
 
44+
**Constraints:**
45+
46+
47+
48+
- ```1 <= nums.length <= 105```
49+
50+
- ```1 <= nums[i] <= 105```
51+
52+
- ```1 <= k <= 105```
53+
54+
55+
56+
## Solution
57+
58+
```javascript
59+
/**
60+
* @param {number[]} nums
61+
* @param {number} k
62+
* @return {number}
63+
*/
64+
var maxFrequency = function(nums, k) {
65+
nums.sort((a, b) => a - b);
66+
67+
var sums = Array(nums.length);
68+
nums.forEach((num, i) => sums[i] = (sums[i - 1] || 0) + num);
69+
70+
var max = 0;
71+
for (var i = 0; i < nums.length; i++) {
72+
var frequency = i + 1 - binarySearch(nums, sums, i, k);
73+
max = Math.max(max, frequency);
74+
}
75+
76+
return max;
77+
};
78+
79+
var binarySearch = function(nums, sums, i, k) {
80+
var left = 0;
81+
var right = i;
82+
var getValue = (j) => {
83+
return nums[i] * (i - j + 1) - (sums[i] - sums[j] + nums[j]);
84+
};
85+
while (left <= right) {
86+
var mid = left + Math.floor((right - left) / 2);
87+
var midValue = getValue(mid);
88+
if (midValue === k) {
89+
return mid;
90+
} else if (midValue > k) {
91+
left = mid + 1;
92+
} else {
93+
if (mid === left) return mid;
94+
right = mid;
95+
}
96+
}
97+
return i;
98+
};
99+
```
100+
101+
**Explain:**
102+
103+
1. sort the array of nums by ascending order
104+
2. calculate prefix sum array
105+
3. for every nums[i], binary search possible index in [0, i],
106+
which can use k operates, to make every num in [index, i] equals nums[i]
107+
108+
**Complexity:**
109+
110+
* Time complexity : O(nlog(n)).
111+
* Space complexity : O(n).

0 commit comments

Comments
 (0)