TheAlgorithms-C/leetcode/src/1838.c
Alexander Pantyukhin b819ddf3e6
feat: add Frequency of the Most Frequent Element (#1195)
* add leetcode Frequency of the Most Frequent Element

* Update 1838.c

small fix

* Update leetcode/src/1838.c

Co-authored-by: David Leal <halfpacho@gmail.com>
2023-01-20 15:42:23 -06:00

37 lines
1.1 KiB
C

#define max(a,b) (((a)>(b))?(a):(b))
int compare(const int* i, const int* j)
{
return *i - *j;
}
// Sort + prefix sum + windows sliding
// Runtime: O(n*log(n))
// Space: O(n)
int maxFrequency(int* nums, int numsSize, int k){
qsort(nums, numsSize, sizeof (int), (int(*) (const void*, const void*)) compare);
long* prefixSum = malloc(numsSize * sizeof(long));
prefixSum[0] = nums[0];
for(int i = 0; i < numsSize - 1; i++){
prefixSum[i + 1] = prefixSum[i] + nums[i];
}
int leftWindowPosition = 0;
int result = 0;
for(int rightWindowPosition = 0; rightWindowPosition < numsSize; rightWindowPosition++){
long rightSum = prefixSum[rightWindowPosition];
long leftSum = prefixSum[leftWindowPosition];
while ((long)nums[rightWindowPosition] * (rightWindowPosition - leftWindowPosition) - (rightSum - leftSum) > k){
leftWindowPosition += 1;
}
result = max(result, rightWindowPosition - leftWindowPosition + 1);
}
free(prefixSum);
return result;
}