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).
classSolution { int dp[200][200]; int dx[4]{0,1,0,-1}, dy[4]{-1,0,1,0}; intdfs(vector<vector<int>>& matrix, int& n, int& m, int y, int x){ if(dp[y][x] != -1) return dp[y][x]; dp[y][x] = 1; for(int i = 0; i < 4; i++) { int ny = y + dy[i], nx = x + dx[i]; if(0<=nx and nx < m and0 <= ny and ny < n and matrix[ny][nx] > matrix[y][x]) { dp[y][x] = max(dp[y][x], dfs(matrix,n,m,ny,nx) + 1); } } return dp[y][x]; } public: intlongestIncreasingPath(vector<vector<int>>& matrix){ memset(dp,-1,sizeof(dp)); int n = matrix.size(), m = matrix[0].size(), res = 0; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) res = max(res, dfs(matrix, n, m, i, j)); return res; } };