-
Notifications
You must be signed in to change notification settings - Fork 118
/
120.c
41 lines (37 loc) · 1.07 KB
/
120.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <stdlib.h>
void print_tri(int **triangle, int numRows) {
int i, j;
for (i = 0; i < numRows; i++) {
for (j = 0; j < numRows; j++) {
printf("%d ", triangle[i][j]);
}
printf("\n");
}
}
int minimumTotal(int **triangle, int numRows) {
int i, j;
for (i = numRows - 1; i > 0; i--) {
for (j = 0; j < i; j++) {
if (triangle[i][j] < triangle[i][j + 1])
triangle[i-1][j] += triangle[i][j];
else
triangle[i-1][j] += triangle[i][j + 1];
//print_tri(triangle, numRows);
}
}
return triangle[0][0];
}
int main() {
int row = 3;
int **triangle = (int **)calloc(row, sizeof(int *));
int i;
for (i = 0; i < row; i++) {
triangle[i] = (int *)calloc(row, sizeof(int));
}
triangle[0][0] = -1;
triangle[1][0] = 2; triangle[1][1] = 3;
triangle[2][0] = 1; triangle[2][1] = -1; triangle[2][2] = -3;
printf("%d\n", minimumTotal(triangle, row));
return 0;
}