Skip to content

Commit 7158ac0

Browse files
authored
Create 0554-Brick-Wall.c
Create the C solution as the same solution in the YouTube video "https://www.youtube.com/watch?v=Kkmv2h48ekw"
1 parent 05f29a2 commit 7158ac0

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

c/0554-Brick-Wall.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#define max(x, y) ((x) > (y) ? (x) : (y))
2+
3+
typedef struct hash_entry {
4+
int position; /* we'll use this field as the key */
5+
int gapCount;
6+
UT_hash_handle hh; /* makes this structure hashable */
7+
} hash_entry;
8+
9+
int leastBricks(int** wall, int wallSize, int* wallColSize){
10+
hash_entry* wallGapCountMap = NULL;
11+
12+
for(int r = 0; r < wallSize; r++)
13+
{
14+
int position = 0;
15+
for(int b = 0; b < *(wallColSize + r) - 1; b++)
16+
{
17+
position += wall[r][b];
18+
19+
hash_entry* retrievedMapEntry;
20+
HASH_FIND_INT(wallGapCountMap, &position, retrievedMapEntry);
21+
22+
// If the position already exists in the map then increment its gap count
23+
if(retrievedMapEntry)
24+
{
25+
retrievedMapEntry->gapCount += 1;
26+
}
27+
else
28+
{
29+
// If the position doesn't exist in the map then create a new map entry for it and add it to the map
30+
hash_entry* mapEntryToAdd = (hash_entry*)malloc(sizeof(hash_entry));
31+
mapEntryToAdd->position = position;
32+
mapEntryToAdd->gapCount = 1;
33+
HASH_ADD_INT(wallGapCountMap, position, mapEntryToAdd);
34+
}
35+
}
36+
}
37+
38+
int maxGap = 0;
39+
for (hash_entry* retrievedMapEntry = wallGapCountMap; retrievedMapEntry != NULL; retrievedMapEntry = retrievedMapEntry->hh.next)
40+
{
41+
maxGap = max(maxGap, retrievedMapEntry->gapCount);
42+
}
43+
44+
return wallSize - maxGap;
45+
}

0 commit comments

Comments
 (0)