Skip to content

Commit

Permalink
Solved LeetCode Longest Increasing Path in a Matrix Problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ghsatpute committed Apr 25, 2023
1 parent 67eabb2 commit a7831cf
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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;
}
}
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
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);
}
}

0 comments on commit a7831cf

Please sign in to comment.