Longest Path in a matrix || GeeksforGeeks || Problem of the Day

preview_player
Показать описание
Longest Path in a matrix || GeeksforGeeks || Problem of the Day

For all GFG courses
get 10% Discount using Coupon code : STUDYWITHSAUMYA

#CodingInterview #GFGPractice #GeeksforGeeks #PracticeProblems #CodingQuestions #leetcode #coding #programming #coding #bfs #graphs #graph #djikstra #gfg #programming #coding #dsa #stack #queue #tree #bst #heap #maxheap #dfs #bfs #backtracking
Рекомендации по теме
Комментарии
Автор

Noice Explanation tbh, your intuition was clear.

iron_nicko
Автор

Excellent . Please be consistent your videos were not there for 3 days.

AnkitSingh-wvxd
Автор

even if you do not include this if condition mention below in the first line of dfs function the code will run perfectly fine.
if (i < 0 || i >= m || j < 0 || j >= n) {
return 0;
}

aman-frrp
Автор

such a good question as well as explanation
and well written code

can u tell me what is the purpose of
if(matrix == null) return 0; in java

RajputAnkit
Автор

could you provide c++ solution as my code is based on same approach as yours, but mine is giving WA.
Here is my code you can look to rectify errors...


vector<vector<int>> direction={{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int matrix) {
// Code here
if( matrix.size()==0)
return 0;
int n=matrix.size(), m=matrix[0].size(), longestPath=0;
vector<vector<int>> cache(n, vector<int> (m, 0));

for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int longest=recursiveFunction(matrix, cache, i, j);
longestPath=max(longest, longestPath);
}
}
return longestPath;
}
int matrix, vector<vector<int>>& cache,
int i, int j)
{
if(cache[i][j]>0)
return cache[i][j];
int n=matrix.size(), m=matrix[0].size(), size=0;
for(int k=0;k<direction.size();k++)
{
int x=i+direction[k][0], y=j+direction[k][1];

{
int longest=recursiveFunction(matrix, cache, x, y);
size=max(size, longest);

}
}
cache[i][j]=size+1;
return cache[i][i];
}

AnkitRaj-myck