- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Longest path in 2-D that contains increasing sequence in JavaScript
Increasing Sequence
A sequence of numbers in which each succeeding element is either greater or equal to the preceding element is an increasing sequence.
For instance,
4, 6, 8, 9, 11, 14 is increasing sequence 3, 3, 3, 3, 3, 3, 3 is also an increasing sequence
Problem:
We are required to write a JavaScript function that takes in a 2-D array of numbers, arr, as the only argument. Our function should find and return the length of that longest path in the array that contains only increasing numbers.
For example, if the input to the function is −
const arr = [ [4, 5, 6], [4, 3, 7], [3, 3, 2] ];
Then the output should be −
const output = 4;
Output Explanation:
Because the longest increasing sequence is 4, 5, 6, 7.
Example
The code for this will be −
const arr = [ [4, 5, 6], [4, 3, 7], [3, 3, 2] ]; const longestIncreasingPath = (arr = []) => { let longest = 0; let dp = Array(arr.length).fill(null).map(() => Array(arr[0].length).fill(1)); const backtracking = (row, col) => { if (dp[row][col]!=1) return dp[row][col]; let dRow = [1,0,-1,0]; let dCol = [0,1,0,-1]; for (let i = 0;i<dRow.length;i++) { let nR = row + dRow[i], nC = col+dCol[i]; if (nR >= 0 && nR < arr.length && nC >= 0 && nC < arr[0].length && arr[nR][nC] > arr[row][col]) { dp[row][col] = Math.max(dp[row][col], 1 + backtracking(nR, nC)) }; }; return dp[row][col]; } for (let i=0;i<arr.length;i++) { for (let j=0;j<arr[0].length;j++) { longest = Math.max(longest, backtracking(i, j)); }; }; return longest; }; console.log(longestIncreasingPath(arr));
Code Explanation:
The Idea
Here, we have used backtracking depth first search.
The recursion function simply returns the longest increasing path for a given row and column
If we already have a record of the longest increasing path for a position, then we can simply return that.
Output
And the output in the console will be −
4
- Related Articles
- Longest Increasing Path in a Matrix in Python
- Longest subarray which only contains strictly increasing numbers JavaScript
- Strictly increasing sequence JavaScript
- Converting array into increasing sequence in JavaScript
- Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays in Python
- Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays in C++
- Finding the longest "uncommon" sequence in JavaScript
- Program to length of longest increasing path in a given matrix in Python
- Total number of longest increasing sequences in JavaScript
- C++ Program to Find the Longest Increasing Subsequence of a Given Sequence
- Longest Increasing Subsequence in Python
- How to get almost increasing sequence of integers in JavaScript ?
- Longest Increasing Subsequence
- Longest Arithmetic Sequence in C++
- Longest Consecutive Sequence in Python
