Skip to content

Commit 06b9f6b

Browse files
refactor 329
1 parent eafd4b1 commit 06b9f6b

File tree

1 file changed

+6
-7
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+6
-7
lines changed

src/main/java/com/fishercoder/solutions/_329.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ public static class Solution1 {
66
final int[] dirs = new int[]{0, 1, 0, -1, 0};
77

88
public int longestIncreasingPath(int[][] matrix) {
9-
if (matrix == null || matrix.length == 0) {
10-
return 0;
11-
}
9+
int m = matrix.length;
10+
int n = matrix[0].length;
1211
int max = 0;
13-
int[][] cache = new int[matrix.length][matrix[0].length];
14-
for (int i = 0; i < matrix.length; i++) {
15-
for (int j = 0; j < matrix[0].length; j++) {
12+
int[][] cache = new int[m][n];
13+
for (int i = 0; i < m; i++) {
14+
for (int j = 0; j < n; j++) {
1615
int len = dfs(matrix, i, j, cache);
1716
max = Math.max(len, max);
1817
}
@@ -25,7 +24,7 @@ int dfs(int[][] matrix, int row, int col, int[][] cache) {
2524
return cache[row][col];
2625
}
2726
int max = 1;
28-
for (int i = 0; i < 4; i++) {
27+
for (int i = 0; i < dirs.length - 1; i++) {
2928
int nextRow = row + dirs[i];
3029
int nextCol = col + dirs[i + 1];
3130
if (nextRow < 0 || nextRow >= matrix.length || nextCol < 0 || nextCol >= matrix[0].length || matrix[nextRow][nextCol] <= matrix[row][col]) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy