Skip to content

Commit d547944

Browse files
Update comments
Signed-off-by: begeekmyfriend <[email protected]>
1 parent 198f46c commit d547944

File tree

4 files changed

+62
-31
lines changed

4 files changed

+62
-31
lines changed

0042_trapping_rain_water/trap_water.c

+27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33

4+
#if 0
5+
static int trap(int* height, int heightSize)
6+
{
7+
int i, res = 0;
8+
int *lmax = malloc(heightSize * sizeof(int));
9+
int *rmax = malloc(heightSize * sizeof(int));
10+
11+
lmax[0] = height[0];
12+
rmax[heightSize - 1] = height[heightSize - 1];
13+
14+
for (i = 1; i < heightSize; i++) {
15+
lmax[i] = height[i] > lmax[i - 1] ? height[i] : lmax[i - 1] ;
16+
}
17+
18+
for (i = heightSize - 2; i >= 0; i--) {
19+
rmax[i] = height[i] > rmax[i + 1] ? height[i] : rmax[i + 1] ;
20+
}
21+
22+
for (i = 1; i < heightSize - 1; i++) {
23+
res += (lmax[i] < rmax[i] ? lmax[i] : rmax[i] ) - height[i];
24+
}
25+
26+
return res;
27+
}
28+
#endif
429

530
static int trap(int* height, int heightSize)
631
{
@@ -12,13 +37,15 @@ static int trap(int* height, int heightSize)
1237
int r = heightSize - 1, rmax = 0;
1338
while (l < r) {
1439
if (height[l] < height[r]) {
40+
/* Only lmax is needed for lmax < rmax here */
1541
if (height[l] > lmax) {
1642
lmax = height[l];
1743
} else {
1844
res += lmax - height[l];
1945
}
2046
l++;
2147
} else {
48+
/* Only rmax is needed for rmax < lmax here */
2249
if (height[r] > rmax) {
2350
rmax = height[r];
2451
} else {

0042_trapping_rain_water/trap_water.cc

+2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ class Solution {
1313
int right = height.size() - 1, right_max = 0;
1414
while (left < right) {
1515
if (height[left] < height[right]) {
16+
/* Only lmax is needed for lmax < rmax here */
1617
if (height[left] > left_max) {
1718
left_max = height[left];
1819
} else {
1920
res += left_max - height[left];
2021
}
2122
left++;
2223
} else {
24+
/* Only rmax is needed for rmax < lmax here */
2325
if (height[right] > right_max) {
2426
right_max = height[right];
2527
} else {

0084_largest_rectangle_in_histogram/rect_in_histogram.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,33 @@
33

44
static int largestRectangleArea(int* heights, int heightsSize)
55
{
6-
int *indexes = malloc(heightsSize * sizeof(int));
7-
int *left = malloc(heightsSize * sizeof(int));
8-
int *right = malloc(heightsSize * sizeof(int));
6+
int *idx_stk = malloc(heightsSize * sizeof(int));
7+
int *lmax = malloc(heightsSize * sizeof(int));
8+
int *rmax = malloc(heightsSize * sizeof(int));
99

1010
int i, pos = 0;
1111
for (i = 0; i < heightsSize; i++) {
12-
/* monotonous increasing stack */
13-
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) {
12+
/* keep monotonous increasing stack */
13+
while (pos > 0 && heights[i] < heights[idx_stk[pos - 1]]) {
1414
pos--;
1515
}
16-
left[i] = pos == 0 ? -1 : indexes[pos - 1];
17-
indexes[pos++] = i;
16+
lmax[i] = pos == 0 ? -1 : idx_stk[pos - 1];
17+
idx_stk[pos++] = i;
1818
}
1919

2020
pos = 0;
2121
for (i = heightsSize - 1; i >= 0; i--) {
22-
/* monotonous increasing stack */
23-
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) {
22+
/* keep monotonous increasing stack */
23+
while (pos > 0 && heights[i] < heights[idx_stk[pos - 1]]) {
2424
pos--;
2525
}
26-
right[i] = pos == 0 ? heightsSize : indexes[pos - 1];
27-
indexes[pos++] = i;
26+
rmax[i] = pos == 0 ? heightsSize : idx_stk[pos - 1];
27+
idx_stk[pos++] = i;
2828
}
2929

3030
int max_area = 0;
3131
for (i = 0; i < heightsSize; i++) {
32-
int area = heights[i] * (right[i] - left[i] - 1);
32+
int area = heights[i] * (rmax[i] - lmax[i] - 1);
3333
max_area = area > max_area ? area : max_area;
3434
}
3535

0085_maximal_rectangle/maximal_rectangle.c

+21-19
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,49 @@ static inline int max(int a, int b)
1010

1111
static int area_calc(int *heights, int size)
1212
{
13-
int *indexes = malloc(size * sizeof(int));
14-
int *lhist = malloc(size * sizeof(int));
15-
int *rhist = malloc(size * sizeof(int));
13+
int *idx_stk = malloc(size * sizeof(int));
14+
int *lmax = malloc(size * sizeof(int));
15+
int *rmax = malloc(size * sizeof(int));
1616

1717
int i, pos = 0;
1818
for (i = 0; i < size; i++) {
19-
/* squeeze to keep monotonous increasing histograms */
20-
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) {
19+
/* keep monotonous increasing maxograms */
20+
while (pos > 0 && heights[i] < heights[idx_stk[pos - 1]]) {
2121
pos--;
2222
}
23-
lhist[i] = pos == 0 ? -1 : indexes[pos - 1];
24-
indexes[pos++] = i;
23+
lmax[i] = pos == 0 ? -1 : idx_stk[pos - 1];
24+
idx_stk[pos++] = i;
2525
}
2626

2727
pos = 0;
2828
for (i = size - 1; i >= 0; i--) {
29-
/* squeeze to keep monotonous increasing histograms */
30-
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) {
29+
/* keep monotonous increasing maxograms */
30+
while (pos > 0 && heights[i] < heights[idx_stk[pos - 1]]) {
3131
pos--;
3232
}
33-
rhist[i] = pos == 0 ? size : indexes[pos - 1];
34-
indexes[pos++] = i;
33+
rmax[i] = pos == 0 ? size : idx_stk[pos - 1];
34+
idx_stk[pos++] = i;
3535
}
3636

3737
int max_area = 0;
3838
for (i = 0; i < size; i++) {
39-
int area = heights[i] * (rhist[i] - lhist[i] - 1);
39+
int area = heights[i] * (rmax[i] - lmax[i] - 1);
4040
max_area = max(area, max_area);
4141
}
4242

4343
return max_area;
4444
}
4545

46-
static int maximalRectangle(char** matrix, int matrixRowSize, int matrixColSize)
46+
static int maximalRectangle(char** matrix, int matrixSize, int* matrixColSize)
4747
{
4848
int i, j, max_area = 0;
49-
int *heights = malloc(matrixColSize * sizeof(int));
50-
memset(heights, 0, matrixColSize * sizeof(int));
51-
for (i = 0; i < matrixRowSize; i++) {
52-
for (j = 0; j < matrixColSize; j++) {
49+
int *heights = malloc(matrixColSize[0] * sizeof(int));
50+
memset(heights, 0, matrixColSize[0] * sizeof(int));
51+
for (i = 0; i < matrixSize; i++) {
52+
for (j = 0; j < matrixColSize[i]; j++) {
5353
heights[j] = matrix[i][j] == '1' ? heights[j] + 1 : 0;
5454
}
55-
max_area = max(max_area, area_calc(heights, matrixColSize));
55+
max_area = max(max_area, area_calc(heights, matrixColSize[i]));
5656
}
5757
return max_area;
5858
}
@@ -68,9 +68,11 @@ int main(int argc, char **argv)
6868
int i, j;
6969
int row_size = argc - 1;
7070
int col_size = strlen(argv[1]);
71+
int *cols = malloc(row_size * sizeof(int));
7172
for (i = 0; i < row_size; i++) {
73+
cols[i] = strlen(argv[1]);
7274
printf("%s\n", argv[i + 1]);
7375
}
74-
printf("%d\n", maximalRectangle(argv + 1, argc - 1, strlen(argv[1])));
76+
printf("%d\n", maximalRectangle(argv + 1, argc - 1, cols));
7577
return 0;
7678
}

0 commit comments

Comments
 (0)