leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 76a1d75d72373fe3dbdc5176b44f39fc7c32e341
parent 630d68a4840a8ae40c063ce36115f401f7273005
Author: Jack Sangdahl <jack@pngu.org>
Date:   Mon, 20 Oct 2025 22:15:56 -0600

3346. maximum frequency of an element after performing operations 1

Diffstat:
Amax_freq_elem_op1.c | 44++++++++++++++++++++++++++++++++++++++++++++
Amax_freq_elem_op1.py | 43+++++++++++++++++++++++++++++++++++++++++++
2 files changed, 87 insertions(+), 0 deletions(-)

diff --git a/max_freq_elem_op1.c b/max_freq_elem_op1.c @@ -0,0 +1,44 @@ +// 3346. Maximum Frequency of an Element After Performing Operations I +#include "leetcode.h" +#include <math.h> + +/* + * you are given an integer array 'nums' and two integers 'k' and + * 'numOperations'. you must perform an operation 'numOperations' times on + * 'nums', where in each operation you select an index 'i' that was not selected + * in any previous operations, and add an integer in the range '[-k, k]' to + * nums. return the maximum possible frequency of any element in 'nums' after + * performing the operations. + */ + +int maxFrequency(int *nums, int numsSize, int k, int numOperations) { + int max = nums[0], ans = 0; + for (int i = 1; i < numsSize; i++) + if (nums[i] > max) + max = nums[i]; + int n = max + k + 2, *freq = (int *)calloc(n, sizeof(int)); + for (int i = 0; i < numsSize; i++) + freq[nums[i]]++; + int *pre = (int *)calloc(n, sizeof(int)); + pre[0] = freq[0]; + for (int i = 1; i < n; i++) + pre[i] = pre[i - 1] + freq[i]; + for (int i = 0; i < n; i++) { + if (!freq[i] && !numOperations) + continue; + int l = fmax(0, i - k), r = fmin(n - 1, i + k); + int total = pre[r] - (l > 0 ? pre[l - 1] : 0); + int adj = total - freq[i]; + int val = freq[i] + fmin(numOperations, adj); + ans = fmax(ans, val); + } + free(freq); + free(pre); + return ans; +} + +int main() { + int n1[] = {1, 4, 5}, n2[] = {5, 11, 20, 20}; + printf("%d\n", maxFrequency(n1, ARRAY_SIZE(n1), 1, 2)); // expect: 2 + printf("%d\n", maxFrequency(n2, ARRAY_SIZE(n2), 5, 1)); // expect: 2 +} diff --git a/max_freq_elem_op1.py b/max_freq_elem_op1.py @@ -0,0 +1,43 @@ +# 3346. Maximum Frequency of an Element After Performing Operations I + +""" +you are given an integer array 'nums' and two integers 'k' and +'numOperations'. you must perform an operation 'numOperations' times on +'nums', where in each operation you select an index 'i' that was not selected +in any previous operations, and add an integer in the range '[-k, k]' to +nums. return the maximum possible frequency of any element in 'nums' after +performing the operations. +""" + + +class Solution(object): + def maxFrequency(self, nums, k, numOperations): + """ + :type nums: List[int] + :type k: int + :type numOperations: int + :rtype: int + """ + mx, ans = max(nums), 0 + n = mx + k + 2 + freq, pre = [0] * n, [0] * n + for i in nums: + freq[i] += 1 + pre[0] = freq[0] + for i in range(1, n): + pre[i] = pre[i - 1] + freq[i] + for i in range(n): + if freq[i] == 0 and numOperations == 0: + continue + l, r = max(0, i - k), min(n - 1, i + k) + total = pre[r] - (pre[l - 1] if l > 0 else 0) + adj = total - freq[i] + val = freq[i] + min(numOperations, adj) + ans = max(ans, val) + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.maxFrequency(nums=[1, 4, 5], k=1, numOperations=2)) + print(obj.maxFrequency(nums=[5, 11, 20, 20], k=5, numOperations=1))