Skip to content

Commit 4af8582

Browse files
committed
Leetcode | Longest Increasing Path in a Matrix | Java
1 parent d0e77eb commit 4af8582

3 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 329. Longest Increasing Path in a Matrix
2+
3+
Given an `m x n` integers `matrix`, return the length of the longest increasing path in `matrix`.
4+
5+
From each cell, you can either move in four directions: left, right, up, or down. You may not move diagonally or move outside the boundary (i.e., wrap-around is not allowed).
6+
7+
Example 1:
8+
9+
Input: matrix = [[9,9,4],[6,6,8],[2,1,1]]
10+
Output: 4
11+
Explanation: The longest increasing path is [1, 2, 6, 9].
12+
13+
Example 2:
14+
15+
Input: matrix = [[3,4,5],[3,2,6],[2,2,1]]
16+
Output: 4
17+
Explanation: The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.
18+
19+
Example 3:
20+
21+
Input: matrix = [[1]]
22+
Output: 1
23+
24+
Constraints:
25+
26+
- `m == matrix.length`
27+
- `n == matrix[i].length`
28+
- `1 <= m, n <= 200`
29+
- `0 <= matrix[i][j] <= 2^31 - 1`
30+
31+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.devstromo.hard.p329;
2+
3+
public class Solution {
4+
private final int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
5+
private int rows, cols;
6+
7+
public int longestIncreasingPath(int[][] matrix) {
8+
rows = matrix.length;
9+
cols = matrix[0].length;
10+
int[][] memo = new int[rows][cols];
11+
int maxPath = 0;
12+
13+
for (int r = 0; r < rows; r++) {
14+
for (int c = 0; c < cols; c++) {
15+
maxPath = Math.max(maxPath, dfs(matrix, r, c, memo));
16+
}
17+
}
18+
19+
return maxPath;
20+
}
21+
22+
private int dfs(int[][] matrix, int r, int c, int[][] memo) {
23+
if (memo[r][c] != 0) return memo[r][c];
24+
25+
int maxLength = 1; // Start with 1 (itself)
26+
for (int[] dir : directions) {
27+
int newRow = r + dir[0];
28+
int newCol = c + dir[1];
29+
30+
if (newRow >= 0 && newRow < rows &&
31+
newCol >= 0 && newCol < cols &&
32+
matrix[newRow][newCol] > matrix[r][c]) {
33+
int length = 1 + dfs(matrix, newRow, newCol, memo);
34+
maxLength = Math.max(maxLength, length);
35+
}
36+
}
37+
38+
memo[r][c] = maxLength;
39+
return maxLength;
40+
}
41+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.devstromo.hard.p329;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class SolutionUnitTest {
9+
private final Solution solution = new Solution();
10+
11+
@Test
12+
@DisplayName("Test Longest Increasing Path in a Matrix")
13+
void testLongestIncreasingPath() {
14+
assertEquals(4, solution.longestIncreasingPath(new int[][]{
15+
{3, 4, 5},
16+
{3, 2, 6},
17+
{2, 2, 1}
18+
}));
19+
assertEquals(1, solution.longestIncreasingPath(new int[][]{
20+
{1}
21+
}));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)