-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberOfIncreasingPathsInAGrid_2328.java
49 lines (45 loc) · 1.55 KB
/
NumberOfIncreasingPathsInAGrid_2328.java
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
42
43
44
45
46
47
48
49
package org.example;
import java.util.Arrays;
public class NumberOfIncreasingPathsInAGrid_2328 {
class Solution {
public int countPaths(int[][] grid) {
int[][] memo = new int[grid.length][grid[0].length];
int[][] visited = new int[grid.length][grid[0].length];
for (int[] row: memo){
Arrays.fill(row, 1);
}
int result = 0;
for (int i=0;i<grid.length;i++){
for (int j=0;j<grid[0].length;j++){
if (visited[i][j]==0){
dfs(memo, visited, i, j);
}
System.out.println(memo[i][j]);
result += memo[i][j];
}
}
return result;
}
public int dfs(int[][] memo, int[][] visited, int i, int j){
int result = 1;
if (visited[i][j] == 1){
return memo[i][j];
}
if (i>0 && memo[i-1][j]<memo[i][j]){
result += dfs(memo, visited, i-1, j);
}
if (i< memo.length-1 && memo[i+1][j]<memo[i][j]){
result += dfs(memo, visited, i+1, j);
}
if (j>0 && memo[i][j-1]<memo[i][j]){
result += dfs(memo, visited, i, j-1);
}
if (j< memo[0].length-1 && memo[i][j+1]<memo[i][j]){
result += dfs(memo, visited, i, j+1);
}
memo[i][j] = result;
visited[i][j] = 1;
return memo[i][j];
}
}
}