
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to length of longest increasing path in a given matrix in Python
Suppose we have a 2D matrix, we have to find the length of the longest strictly increasing path. To traverse the path we can move up, down, left, or right nor diagonally.
So, if the input is like
2 | 4 | 6 |
1 | 5 | 7 |
3 | 3 | 9 |
then the output will be 6, as The longest path is [1, 2, 4, 6, 7, 9]
To solve this, we will follow these steps −
n := row count of matrix , m := column count of matrix moves := a list of pairs to move up, down, left and right [[1, 0], [-1, 0], [0, 1], [0, -1]] Define a function dp() . This will take y, x if x and y are in range of matrix, then return 0 currVal := matrix[y, x] res := 0 for each d in moves, do (dy, dx) := d (newY, newX) := (y + dy, x + dx) if newY and newX are in range of matrix and matrix[newY, newX] > currVal, then res := maximum of res and dp(newY, newX) return res + 1 From the main method do the following: result := 0 for i in range 0 to n - 1, do for j in range 0 to m - 1, do result := maximum of result and dp(i, j) return result
Example (Python)
Let us see the following implementation to get better understanding −
class Solution: def solve(self, matrix): n, m = len(matrix), len(matrix[0]) moves = [[1, 0], [-1, 0], [0, 1], [0, -1]] def dp(y, x): if y < 0 or y >= n or x < 0 or x >= m: return 0 currVal = matrix[y][x] res = 0 for d in moves: dy, dx = d newY, newX = y + dy, x + dx if (newY >= 0 and newY < n and newX >= 0 and newX < m and matrix[newY][newX] > currVal): res = max(res, dp(newY, newX)) return res + 1 result = 0 for i in range(n): for j in range(m): result = max(result, dp(i, j)) return result ob = Solution() matrix = [ [2, 4, 6], [1, 5, 7], [3, 3, 9] ] print(ob.solve(matrix))
Input
[ [2, 4, 6], [1, 5, 7], [3, 3, 9] ]
Output
6
- Related Articles
- Longest Increasing Path in a Matrix in Python
- Program to find length of longest matrix path length in Python
- Program to find length of longest increasing subsequence in Python
- Program to find length of longest circular increasing subsequence in python
- Program to find length of longest consecutively increasing substring in Python
- Program to find length of longest path with even sum in Python
- Program to find length of longest alternating path of a binary tree in python
- Program to find length of longest consecutive path of a binary tree in python
- Program to find length of longest strictly increasing then decreasing sublist in Python
- Program to find length of longest contiguously strictly increasing sublist after removal in Python
- Program to find length of the longest path in a DAG without repeated nodes in Python
- Program to find length of the longest path in an n-ary tree in Python
- Find the longest path in a matrix with given constraints in C++
- Program to find length of longest arithmetic subsequence of a given list in Python
- Program to find length of longest sublist with given condition in Python

Advertisements