File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments