-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC2684.java
More file actions
63 lines (51 loc) · 1.74 KB
/
LC2684.java
File metadata and controls
63 lines (51 loc) · 1.74 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* LC2684
*/
import java.util.*;
public class LC2684 {
public static int maxMoves(int[][] grid) {
int ans = 0;
int dp[][] = new int[grid.length][grid[0].length];
for (int i = 0; i < grid.length; i++) {
ans = Math.max(ans, solve(grid, i, 0, dp));
}
return ans;
}
public static int solve(int grid[][], int row, int col, int dp[][]) {
// Base Case
if (dp[row][col] != 0) {
return dp[row][col];
}
int pos1 = 0;
int pos2 = 0;
int pos3 = 0;
if (row - 1 >= 0 && col + 1 < grid[0].length && grid[row][col] < grid[row - 1][col + 1]) {
pos1 = 1 + solve(grid, row - 1, col + 1, dp);
}
if (col + 1 < grid[0].length && grid[row][col] < grid[row][col + 1]) {
pos2 = 1 + solve(grid, row, col + 1, dp);
}
if (row + 1 < grid.length && col + 1 < grid[0].length && grid[row][col] < grid[row + 1][col + 1]) {
pos3 = 1 + solve(grid, row + 1, col + 1, dp);
}
return dp[row][col] = Math.max(pos1, Math.max(pos2, pos3));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Matrix Size : ");
int n = sc.nextInt();
System.out.println();
int[][] arr = new int[n][n];
System.out.println("Enter The Matrix Elements : ");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.printf("[%d][%d] : ", i, j);
arr[i][j] = sc.nextInt();
}
}
System.out.println();
int ans = maxMoves(arr);
System.out.println(ans);
sc.close();
}
}