Skip to content

Commit 77696ee

Browse files
authored
Create 0347-Top-K-Frequent-Elements.c
Create the C solution as the same solution in the YouTube video "https://www.youtube.com/watch?v=YPTqKIgVk-k"
1 parent efc867c commit 77696ee

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

c/0347-Top-K-Frequent-Elements.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#define ALLOCATION_SIZE 100 // Allocation size of the numsArrLen in frequencyArrItem
2+
3+
typedef struct hash_entry {
4+
int num; /* we'll use this field as the key */
5+
int count;
6+
UT_hash_handle hh; /* makes this structure hashable */
7+
}hash_entry;
8+
9+
typedef struct frequencyArrItem
10+
{
11+
int* numsArr;
12+
int numsArrLen;
13+
int index; // Points to the first empty space in numsArr
14+
}frequencyArrItem;
15+
16+
/**
17+
* Note: The returned array must be malloced, assume caller calls free().
18+
*/
19+
int* topKFrequent(int* nums, int numsSize, int k, int* returnSize){
20+
int* result = (int*)malloc(k * sizeof(int));
21+
*returnSize = k;
22+
int resultIndex = 0; // Points to the first empty space in result
23+
hash_entry* NumFreqMap = NULL;
24+
frequencyArrItem frequencyArr[numsSize + 1]; // Index is the count/frequency and the element is an array of numbers with this frequency
25+
memset(frequencyArr, 0, (numsSize + 1) * sizeof(frequencyArrItem)); // Initialize with NULL and zeros
26+
27+
for(int i = 0; i < numsSize; i++)
28+
{
29+
hash_entry* retrievedMapEntry;
30+
HASH_FIND_INT(NumFreqMap, &nums[i], retrievedMapEntry);
31+
32+
// If the number already exists in the map then increment its count
33+
if(retrievedMapEntry)
34+
{
35+
retrievedMapEntry->count += 1;
36+
}
37+
else
38+
{
39+
// If the number doesn't exist in the map then create a new map entry for it and add it to the map
40+
hash_entry* mapEntryToAdd = (hash_entry*)malloc(sizeof(hash_entry));
41+
mapEntryToAdd->num = nums[i];
42+
mapEntryToAdd->count = 1;
43+
HASH_ADD_INT(NumFreqMap, num, mapEntryToAdd);
44+
}
45+
}
46+
47+
// Loop over all the entries in the hash map
48+
for (hash_entry* mapEntry = NumFreqMap; mapEntry != NULL; mapEntry = mapEntry->hh.next) {
49+
frequencyArrItem* freq = frequencyArr + mapEntry->count;
50+
51+
// If the nums list for this frequency has not been created yet
52+
if(freq->numsArrLen == 0)
53+
{
54+
freq->numsArrLen = ALLOCATION_SIZE;
55+
freq->numsArr = (int*)malloc(freq->numsArrLen * sizeof(int));
56+
}
57+
// If the nums list exceeded the current allocated size, then reallocate
58+
else if(freq->index == freq->numsArrLen)
59+
{
60+
freq->numsArrLen += ALLOCATION_SIZE;
61+
freq->numsArr = (int*)realloc(freq->numsArr, freq->numsArrLen * sizeof(int));
62+
}
63+
64+
freq->numsArr[freq->index++] = mapEntry->num;
65+
}
66+
67+
// Loop over the frequencies starting from the highest frequency
68+
for(int i = numsSize; i >= 1; i--) // Note that we are looping until i == 1, as at index 0 it is just empty
69+
{
70+
frequencyArrItem* freq = frequencyArr + i;
71+
// If there is nums that exist at this particular frequency
72+
for(int j = 0; j < freq->index; j++)
73+
{
74+
// If we have added all the elements we need to the result
75+
if(k-- == 0)
76+
{
77+
return result;
78+
}
79+
80+
// Add the num to the result then increment the index
81+
result[resultIndex++] = freq->numsArr[j];
82+
}
83+
}
84+
85+
return result;
86+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy