forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Leo Ma <[email protected]>
- Loading branch information
1 parent
2fde4f6
commit c923ab2
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test triangle.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
#include <string.h> | ||
|
||
static int recursive(int** triangle, int row_size, int *col_sizes, | ||
int row, int col, int **sums, bool **passes) | ||
{ | ||
if (row == row_size - 1) { | ||
return triangle[row][col]; | ||
} else if (passes[row][col]) { | ||
return sums[row][col]; | ||
} else { | ||
int s1 = recursive(triangle, row_size, col_sizes, row + 1, col, sums, passes); | ||
int s2 = recursive(triangle, row_size, col_sizes, row + 1, col + 1, sums, passes); | ||
sums[row][col] = triangle[row][col] + (s1 < s2 ? s1 : s2); | ||
passes[row][col] = true; | ||
return sums[row][col]; | ||
} | ||
} | ||
|
||
static int minimumTotal(int** triangle, int triangleRowSize, int *triangleColSizes) | ||
{ | ||
int i; | ||
bool **passes = malloc(triangleRowSize * sizeof(bool *)); | ||
for (i = 0; i < triangleRowSize; i++) { | ||
passes[i] = malloc(triangleColSizes[i]); | ||
memset(passes[i], false, triangleColSizes[i]); | ||
} | ||
int **sums = malloc(triangleRowSize * sizeof(int *)); | ||
for (i = 0; i < triangleRowSize; i++) { | ||
sums[i] = malloc(triangleColSizes[i] * sizeof(int)); | ||
} | ||
return recursive(triangle, triangleRowSize, triangleColSizes, 0, 0, sums, passes); | ||
} | ||
|
||
int main(void) | ||
{ | ||
int i, row = 4; | ||
int **nums = malloc(row * sizeof(int *)); | ||
int *sizes = malloc(row * sizeof(int)); | ||
for (i = 0; i < row; i++) { | ||
sizes[i] = i + 1; | ||
nums[i] = malloc(sizes[i] * sizeof(int)); | ||
} | ||
#if 0 | ||
nums[0][0] = -1; | ||
nums[1][0] = 3; | ||
nums[1][1] = 2; | ||
nums[2][0] = -3; | ||
nums[2][1] = 1; | ||
nums[2][2] = -1; | ||
#else | ||
nums[0][0] = 2; | ||
nums[1][0] = 3; | ||
nums[1][1] = 4; | ||
nums[2][0] = 6; | ||
nums[2][1] = 5; | ||
nums[2][2] = 7; | ||
nums[3][0] = 4; | ||
nums[3][1] = 1; | ||
nums[3][2] = 8; | ||
nums[3][3] = 3; | ||
#endif | ||
printf("%d\n", minimumTotal(nums, row, sizes)); | ||
return 0; | ||
} |