Link: https://leetcode.com/problems/maximum-frequency-after-subarray-operation
| Date | Stopwatch | Y/N | Feedback |
|---|---|---|---|
| Jun 17, 2025 |
Intuitively, we want to know how many k already exist in nums, so we can first count how many k we have so far, then find the most k we can make from nums.
-
Loop over
numsto find the total of existedk. -
Use global variable
ansto save the most extrakwe can make fromnums. Since the constraint is1 <= nums[i] <= 50, so we can try eachvalfrom[1, 50]and see whichvalexists the most innums. And we can use Kadane's algorithm to find the most countedvalby checking eachnuminnums, ifval == num, increment the count, ifval == k, decrement the count because it will not bekafter operation.
We updatecnt_freq, cnt_maxeach time when we are loopingnums, because by Kadane's algorithm, ifcnt_freq < 0, means we should skip this current subarray ofnums, we keepcnt_freq = max(cnt_freq, 0), if it leads to negative, we rather don't take this subarray. We updatecnt_maxat the same time, if currentcnt_freqleads to higher count. However, we only updateansafter we finish loopingnums, ifcnt_max > answe know there is a subarray fromnumswith currentvalcan apply operation to get morek. -
Return
ans + k_cnt, the number of existedkinnums+ how many elements we can apply operation to lead to the max frequency ofk.
class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
# 1. Count how many k we have in nums
# 2. Since we want to find the mode in nums, brute force to try val from [1, 50], if num in nums == val, increment cnt, if num == k, decrement cnt, because k will not be k after increment of val
# TC: O(n), n=len(nums), SC: O(1)
# Count how many k in nums
k_cnt = 0
for num in nums:
if num == k:
k_cnt += 1
ans = 0
# Try val from[1, 50]
for val in range(1, 51):
# Count how many num in nums == val
if val == k:
continue
cnt_freq, cnt_max = 0, 0
# Loop over nums
for num in nums:
if num == val:
cnt_freq += 1
elif num == k:
cnt_freq -= 1
# Kadane's algorithm, if this leads to negative, rather just don't take them
cnt_freq = max(cnt_freq, 0)
cnt_max = max(cnt_max, cnt_freq)
ans = max(ans, cnt_max)
return ans + k_cntTime Complexity:
Space Complexity:
class Solution {
public:
int maxFrequency(vector<int>& nums, int k) {
/*
1. Count existed k in nums
2. Loop over [1, 50], choose the one with most frequent in nums
3. Return k_cnt + most_cnt
TC: O(n), SC: O(1)
*/
int k_cnt = 0, max_cnt = 0;
// Count total existed k in nums
for (int num: nums) {
if (num == k) {
k_cnt++;
}
}
// Loop over [1, 50]
for (int i = 0; i < 51; i++) {
// Skip if i == k
if (i == k) {
continue;
}
// Count the most frequent subarray of i in nums
int cnt_freq = 0, cnt_max = 0;
for (int num: nums) {
if (num == i) {
cnt_freq++;
} else if (num == k) {
cnt_freq--;
}
// Kadane's algorithm, if cnt_freq < 0, rather don't take subarray
cnt_freq = std::max(cnt_freq, 0);
cnt_max = std::max(cnt_freq, cnt_max);
}
// After current i, we will find the most frequent subarray of i
max_cnt = std::max(max_cnt, cnt_max);
}
return k_cnt + max_cnt;
}
};