Skip to content

Commit

Permalink
Create 0554-Brick-Wall.c
Browse files Browse the repository at this point in the history
Create the C solution as the same solution in the YouTube video "https://www.youtube.com/watch?v=Kkmv2h48ekw"
  • Loading branch information
MHamiid authored Jan 14, 2023
1 parent 05f29a2 commit 7158ac0
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions c/0554-Brick-Wall.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#define max(x, y) ((x) > (y) ? (x) : (y))

typedef struct hash_entry {
int position; /* we'll use this field as the key */
int gapCount;
UT_hash_handle hh; /* makes this structure hashable */
} hash_entry;

int leastBricks(int** wall, int wallSize, int* wallColSize){
hash_entry* wallGapCountMap = NULL;

for(int r = 0; r < wallSize; r++)
{
int position = 0;
for(int b = 0; b < *(wallColSize + r) - 1; b++)
{
position += wall[r][b];

hash_entry* retrievedMapEntry;
HASH_FIND_INT(wallGapCountMap, &position, retrievedMapEntry);

// If the position already exists in the map then increment its gap count
if(retrievedMapEntry)
{
retrievedMapEntry->gapCount += 1;
}
else
{
// If the position doesn't exist in the map then create a new map entry for it and add it to the map
hash_entry* mapEntryToAdd = (hash_entry*)malloc(sizeof(hash_entry));
mapEntryToAdd->position = position;
mapEntryToAdd->gapCount = 1;
HASH_ADD_INT(wallGapCountMap, position, mapEntryToAdd);
}
}
}

int maxGap = 0;
for (hash_entry* retrievedMapEntry = wallGapCountMap; retrievedMapEntry != NULL; retrievedMapEntry = retrievedMapEntry->hh.next)
{
maxGap = max(maxGap, retrievedMapEntry->gapCount);
}

return wallSize - maxGap;
}

0 comments on commit 7158ac0

Please sign in to comment.