-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solved LeetCode Longest Increasing Path in a Matrix Problem
- Loading branch information
Showing
5 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
Binary file added
BIN
+13.8 KB
...etcode/algorithm/dynamicProgramming/longestIncreasingPathInAMatrix/Example1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.3 KB
...etcode/algorithm/dynamicProgramming/longestIncreasingPathInAMatrix/Example2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions
64
...ming/longestIncreasingPathInAMatrix/LongestIncreasingPathInAMatrixDynamicProgramming.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package problemsolving.leetcode.algorithm.dynamicProgramming.longestIncreasingPathInAMatrix; | ||
|
||
public class LongestIncreasingPathInAMatrixDynamicProgramming { | ||
private int[][] matrix; | ||
private int[][] dp; | ||
private int numRows; | ||
private int numCols; | ||
private int maxAnswer; | ||
|
||
public int longestIncreasingPath(int[][] matrix) { | ||
this.matrix = matrix; | ||
this.numRows = matrix.length; | ||
this.numCols = matrix[0].length; | ||
this.dp = new int[numRows][numCols]; | ||
for (int i = 0; i < numRows; i++) { | ||
for (int j = 0; j < numCols; j++) { | ||
dfs(i, j); | ||
} | ||
} | ||
|
||
return maxAnswer; | ||
} | ||
|
||
private int dfs(int row, int col) { | ||
if (row > numRows - 1 || col > numCols - 1) { | ||
return 0; | ||
} | ||
// For single element, at least path of 1 length will be there, | ||
// so if value is not zero means we've visited this cell | ||
if (dp[row][col] != 0) { | ||
return dp[row][col]; | ||
} | ||
|
||
// Single element is always part longest increasing path of length 1 | ||
int answer = 1; | ||
// Try to visit left cell | ||
if (col > 0 && matrix[row][col - 1] > matrix[row][col]) { | ||
int leftAnswer = 1 + dfs(row, col - 1); | ||
answer = Math.max(answer, leftAnswer); | ||
} | ||
|
||
// Try to visit right cell | ||
if (col < numCols - 1 && matrix[row][col + 1] > matrix[row][col]) { | ||
int rightAnswer = 1 + dfs(row, col + 1); | ||
answer = Math.max(answer, rightAnswer); | ||
} | ||
|
||
// Try to visit top cell | ||
if (row > 0 && matrix[row - 1][col] > matrix[row][col]) { | ||
int topAnswer = 1 + dfs(row - 1, col); | ||
answer = Math.max(answer, topAnswer); | ||
} | ||
|
||
// Try to visit bottom cell | ||
if (row < numRows - 1 && matrix[row + 1][col] > matrix[row][col]) { | ||
int bottomAnswer = 1 + dfs(row + 1, col); | ||
answer = Math.max(answer, bottomAnswer); | ||
} | ||
dp[row][col] = answer; | ||
maxAnswer = Math.max(maxAnswer, answer); | ||
|
||
return answer; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../leetcode/algorithm/dynamicProgramming/longestIncreasingPathInAMatrix/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# 329. Longest Increasing Path in a Matrix | ||
|
||
Given an `m x n` integers `matrix`, return the length of the longest | ||
increasing path in matrix. | ||
|
||
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). | ||
|
||
### Example 1 | ||
![Example1.png](Example1.png) | ||
``` | ||
Input: matrix = [[9,9,4],[6,6,8],[2,1,1]] | ||
Output: 4 | ||
Explanation: The longest increasing path is [1, 2, 6, 9]. | ||
``` | ||
|
||
### Example 2 | ||
![Example2.png](Example2.png) | ||
``` | ||
Input: matrix = [[3,4,5],[3,2,6],[2,2,1]] | ||
Output: 4 | ||
Explanation: The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed. | ||
``` | ||
|
||
### Example 3 | ||
```Input: matrix = [[1]] | ||
Output: 1 | ||
``` | ||
|
||
## Constraints | ||
* `m == matrix.length` | ||
* `n == matrix[i].length` | ||
* `1 <= m, n <= 200` | ||
* `0 <= matrix[i][j] <= 2^31 - 1` | ||
|
||
# Solution Reference | ||
https://www.youtube.com/watch?v=wCc_nd-GiEc&ab_channel=NeetCode |
42 changes: 42 additions & 0 deletions
42
.../longestIncreasingPathInAMatrix/LongestIncreasingPathInAMatrixDynamicProgrammingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package problemsolving.leetcode.algorithm.dynamicProgramming.longestIncreasingPathInAMatrix; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class LongestIncreasingPathInAMatrixDynamicProgrammingTest { | ||
@Test | ||
public void testCase01() { | ||
int[][] matrix = { | ||
{9, 9, 4}, | ||
{6, 6, 8}, | ||
{2, 1, 1} | ||
}; | ||
|
||
int output = new LongestIncreasingPathInAMatrixDynamicProgramming().longestIncreasingPath(matrix); | ||
|
||
assertEquals(4, output); | ||
} | ||
|
||
@Test | ||
public void testCase02() { | ||
int[][] matrix = { | ||
{3, 4, 5}, | ||
{3, 2, 6}, | ||
{2, 2, 1} | ||
}; | ||
|
||
int output = new LongestIncreasingPathInAMatrixDynamicProgramming().longestIncreasingPath(matrix); | ||
|
||
assertEquals(4, output); | ||
} | ||
|
||
@Test | ||
public void testCase03() { | ||
int[][] matrix = {{1}}; | ||
|
||
int output = new LongestIncreasingPathInAMatrixDynamicProgramming().longestIncreasingPath(matrix); | ||
|
||
assertEquals(1, output); | ||
} | ||
} |