function longestIncreasingPath(matrix) {
let max = 1;
let row = matrix.length;
let column = matrix[0].length;
let arr = [];
for(let i = 0; i < row; i++) {
arr.push([])
for(let j = 0; j < column; j++) {
arr[i][j] = 0
}
}
for(let i = 0; i < row; i++) {
for(let j = 0; j < column; j++) {
max = Math.max(max, search(i, j, matrix, arr));
}
}
return max
};
function search(i, j, matrix, arr) {
let left = 1;
let right = 1;
let top = 1;
let bottom = 1;
if(i - 1 >= 0 && matrix[i][j] < matrix[i - 1][j]) {
if(arr[i - 1][j]) {
top = arr[i - 1][j] + 1
}else {
top = search(i - 1, j, matrix, arr) + 1;
}
}
if(i + 1 < matrix.length && matrix[i][j] < matrix[i + 1][j]) {
if(arr[i + 1][j]) {
bottom = arr[i + 1][j] + 1
}else {
bottom = search(i + 1, j, matrix, arr) + 1;
}
}
if(j - 1 >= 0 && matrix[i][j] < matrix[i][j - 1]) {
if(arr[i][j - 1]) {
left = arr[i][j - 1] + 1
}else {
left = search(i, j - 1, matrix, arr) + 1;
}
}
if(j + 1 < matrix[0].length && matrix[i][j] < matrix[i][j + 1]) {
if(arr[i][j + 1]) {
right = arr[i][j + 1] + 1
}else {
right = search(i, j + 1, matrix, arr) + 1;
}
}
return Math.max(left, right, top, bottom)
}
let arr = [[9,9,4],[6,6,8],[2,1,1]]
console.log(longestIncreasingPath(arr))
console