Input: nums =[1,4,5], k =1, numOperations =2Output: 2Explanation:
We can achieve a maximum frequency of two by:* Adding 0 to `nums[1]`, after which `nums` becomes `[1, 4, 5]`.* Adding -1 to `nums[2]`, after which `nums` becomes `[1, 4, 4]`.
To maximize the frequency of any element after performing up to numOperations operations (each changing a different index by at most k), we want to make as many elements as possible equal to some value. By sorting the array, we can use a sliding window and binary search to efficiently check, for each possible target, how many elements can be made equal to it within the allowed operations, considering the cost of increasing and decreasing values.
Use prefix sums to quickly compute the sum of any subarray.
For each index r, use binary search to find the leftmost index l such that the total cost to make all elements in nums[l..r] equal to nums[r] is within numOperations * k.
The cost is nums[r] * (r-l+1) - (prefix[r+1] - prefix[l]).
classSolution {
public:int maxFrequency(vector<int>& nums, int k, int numOperations) {
sort(nums.begin(), nums.end());
int n = nums.size(), ans =1;
vector<longlong> pre(n+1);
for (int i =0; i < n; ++i) pre[i+1] = pre[i] + nums[i];
for (int r =0; r < n; ++r) {
int l =0, m;
int lo =0, hi = r;
while (lo <= hi) {
m = (lo + hi) /2;
longlong cost =1LL* nums[r] * (r-m+1) - (pre[r+1] - pre[m]);
if (cost <=1LL* numOperations * k) hi = m -1;
else lo = m +1;
}
ans = max(ans, r - lo +1);
}
return ans;
}
};
funcmaxFrequency(nums []int, kint, numOperationsint) int {
sort.Ints(nums)
n:= len(nums)
pre:= make([]int64, n+1)
fori:=0; i < n; i++ {
pre[i+1] = pre[i] + int64(nums[i])
}
ans:=1forr:=0; r < n; r++ {
lo, hi:=0, rforlo<=hi {
m:= (lo+hi) /2cost:= int64(nums[r]) * int64(r-m+1) - (pre[r+1] -pre[m])
ifcost<= int64(numOperations)*int64(k) {
hi = m-1 } else {
lo = m+1 }
}
ifr-lo+1 > ans {
ans = r-lo+1 }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
publicintmaxFrequency(int[] nums, int k, int numOperations) {
Arrays.sort(nums);
int n = nums.length, ans = 1;
long[] pre =newlong[n+1];
for (int i = 0; i < n; i++) pre[i+1]= pre[i]+ nums[i];
for (int r = 0; r < n; r++) {
int lo = 0, hi = r, m;
while (lo <= hi) {
m = (lo + hi) / 2;
long cost = 1L * nums[r]* (r-m+1) - (pre[r+1]- pre[m]);
if (cost <= 1L * numOperations * k) hi = m - 1;
else lo = m + 1;
}
ans = Math.max(ans, r - lo + 1);
}
return ans;
}
}
classSolution {
funmaxFrequency(nums: IntArray, k: Int, numOperations: Int): Int {
nums.sort()
val n = nums.size
val pre = LongArray(n+1)
for (i in0 until n) pre[i+1] = pre[i] + nums[i]
var ans = 1for (r in0 until n) {
var lo = 0var hi = r
while (lo <= hi) {
val m = (lo + hi) / 2val cost = nums[r].toLong() * (r-m+1) - (pre[r+1] - pre[m])
if (cost <= numOperations.toLong() * k) hi = m - 1else lo = m + 1 }
ans = maxOf(ans, r - lo + 1)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
defmaxFrequency(self, nums: list[int], k: int, numOperations: int) -> int:
nums.sort()
n = len(nums)
pre = [0]
for v in nums:
pre.append(pre[-1] + v)
ans =1for r in range(n):
lo, hi =0, r
while lo <= hi:
m = (lo + hi) //2 cost = nums[r] * (r-m+1) - (pre[r+1] - pre[m])
if cost <= numOperations * k:
hi = m -1else:
lo = m +1 ans = max(ans, r - lo +1)
return ans
impl Solution {
pubfnmax_frequency(nums: Vec<i32>, k: i32, num_operations: i32) -> i32 {
letmut nums = nums;
nums.sort();
let n = nums.len();
letmut pre =vec![0i64; n+1];
for i in0..n {
pre[i+1] = pre[i] + nums[i] asi64;
}
letmut ans =1;
for r in0..n {
let (mut lo, mut hi) = (0, r);
while lo <= hi {
let m = (lo + hi) /2;
let cost = nums[r] asi64* (r asi64- m asi64+1) - (pre[r+1] - pre[m]);
if cost <= num_operations asi64* k asi64 {
hi = m -1;
} else {
lo = m +1;
}
}
ans = ans.max(r asi32- lo asi32+1);
}
ans
}
}