forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0329-longest-increasing-path-in-a-matrix.c
40 lines (36 loc) · 1.13 KB
/
0329-longest-increasing-path-in-a-matrix.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
/*
Given an m x n integers matrix, return the length of the longest increasing path in matrix.
Time; O(n*m)
Space; O(n*m)
*/
int max(int a, int b) {
return a>b?a:b;
}
int dfs(int** matrix, int** dp, int n, int m, int i, int j) {
if (dp[i][j]!=0)
return dp[i][j];
int cpt=0;
if (i>0 && matrix[i-1][j]>matrix[i][j])
cpt = max(cpt, dfs(matrix, dp, n, m, i-1, j));
if (j>0 && matrix[i][j-1]>matrix[i][j])
cpt = max(cpt, dfs(matrix, dp, n, m, i, j-1));
if (i<(n-1) && matrix[i+1][j]>matrix[i][j])
cpt = max(cpt, dfs(matrix, dp, n, m, i+1, j));
if (j<(m-1) && matrix[i][j+1]>matrix[i][j])
cpt = max(cpt, dfs(matrix, dp, n, m, i, j+1));
dp[i][j] = cpt+1;
return cpt+1;
}
int longestIncreasingPath(int** matrix, int matrixSize, int* matrixColSize){
int** dp = malloc(sizeof(int*)*matrixSize);
int m = *matrixColSize, i, j;
for (i=0; i<matrixSize; i++)
dp[i] = calloc(m, sizeof(int));
int ans = 0;
for (i=0; i<matrixSize; i++) {
for (j=0; j<m; j++) {
ans = max(ans, dfs(matrix, dp, matrixSize, m, i, j));
}
}
return ans;
}