您的位置:首页 > 编程语言 > C语言/C++

leetcode_[python/C++] 329. Longest Increasing Path in a Matrix(DFS+记忆化搜索)

2016-11-16 01:15 381 查看
题目链接

【题目】

Given an integer matrix, find the length of the longest increasing path.

From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).

Example 1:

nums = [

[9,9,4],

[6,6,8],

[2,1,1]

]

Return 4

The longest increasing path is [1, 2, 6, 9].

Example 2:

nums = [

[3,4,5],

[3,2,6],

[2,2,1]

]

Return 4

The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.

【分析】看到这道题第一反应是纯粹的DFS肯定会超时,思考了很久之后发现每一个搜索的位置,且以该位置为起点的最长路径都是可用的,也就是搜索过的位置都能够在下一次搜索到这个位置时直接与已走的路程相加,从而形成一种记忆化的搜索,明显提高了效率

DFS+记忆化搜索:

二维lens记录以每一个位置为起点的最大路径长度

二维visited记录深度搜索过程中已被访问与重置

备注

整个程序写完之后发现超时了,leetcode给了我一个超大的输入样例给我当头一棒,百思不得姐

[[13,2,12,10,15,15,6,10,15,18,16,15,19,17,8,2,8,5,13,13,16,13,2,18,9,9,4,5,1,18,10,1,19,15,6,7,4,18,0,16,19,1,0,17,10,5,17,12,13,6,17,16,18,9,17,17,12,15,1],[19,18,1,17,19,16,5,11,18,3,17,18,13,10,11,8,16,13,13,19,18,7,2,14,6,5,19,6,6,4,1,13,9,0,15,12,19,11,12,0,1,18,2,11,5,4,14,1,5,12,14,19,4,16,1,12,19,8,10],[13,15,10,14,11,15,9,19,9,1,16,4,5,1,7,19,9,9,5,19,6,13,15,7,11,15,6,14,16,19,16,17,11,6,8,19,8,13,19,6,17,6,11,16,1,17,15,11,19,5,6,17,16,2,9,3,0,7,8],[18,6,14,17,6,2,7,8,16,4,11,11,6,17,9,17,1,13,7,10,2,5,12,14,5,14,1,11,4,19,10,11,5,1,16,19,17,19,5,17,12,14,12,0,17,11,18,1,14,5,10,16,8,2,6,9,15,13,10],[17,1,6,7,10,2,5,12,10,0,6,8,0,8,19,6,11,13,13,10,0,16,15,1,8,6,8,11,12,19,16,9,7,15,15,11,1,14,3,12,5,0,4,1,7,12,14,15,9,12,17,16,12,3,6,2,0,10,18],[4,14,8,16,18,17,0,12,0,4,9,15,14,1,18,7,15,8,16,15,0,8,9,9,13,15,2,12,15,18,10,12,1,19,2,16,9,2,6,3,17,7,2,16,4,2,5,1,11,7,7,0,14,4,12,3,4,5,7],[1,10,6,9,10,5,11,15,1,5,7,15,2,19,11,2,15,4,0,9,4,12,2,5,18,3,3,10,1,14,12,3,6,9,10,15,0,8,7,5,3,12,16,13,15,1,7,13,19,3,18,2,5,3,11,0,0,16,15],[17,1,13,10,17,11,13,0,0,18,3,6,5,13,4,1,3,12,12,16,10,9,17,1,12,13,18,17,6,15,17,13,11,1,8,18,19,19,6,17,16,16,5,4,10,7,3,9,6,3,15,13,6,0,14,17,11,10,16],[1,7,6,9,1,7,16,1,0,18,2,12,9,0,2,14,2,1,10,3,1,16,19,11,14,16,1,10,2,4,18,10,1,5,6,6,3,9,9,16,13,17,13,19,4,10,12,19,11,10,14,17,5,15,11,14,11,18,3],[13,12,9,16,10,19,2,14,11,14,5,2,5,4,17,16,10,10,8,10,15,15,8,2,17,1,9,9,15,8,1,15,14,18,4,15,8,6,14,16,19,3,15,14,9,1,6,2,19,18,9,0,11,18,8,15,19,5,0],[16,19,18,12,9,3,5,0,1,19,11,7,0,4,6,3,0,19,1,7,8,9,17,1,14,10,5,17,16,15,0,7,12,4,0,9,0,3,13,1,1,11,5,19,3,0,19,14,2,8,2,17,5,15,19,0,16,16,19],[17,10,1,11,14,1,10,3,9,16,1,14,8,2,17,8,6,1,16,14,7,11,15,10,8,14,7,19,3,7,18,12,18,12,5,6,6,18,8,13,11,2,18,19,19,10,12,12,14,13,17,10,18,19,13,9,7,18,7],[15,16,5,16,1,4,19,6,9,13,13,2,12,5,10,18,10,16,8,12,14,5,15,13,8,11,18,5,13,11,14,8,9,9,11,4,16,5,2,14,7,17,5,13,4,12,7,6,5,2,16,6,13,15,8,5,8,6,4],[15,8,8,5,6,14,5,10,0,12,15,2,7,2,4,18,4,15,3,5,4,1,13,17,9,17,1,0,16,17,11,1,12,9,5,8,1,13,8,16,16,6,14,2,15,12,0,12,19,19,0,18,1,13,13,5,6,15,11],[10,12,9,11,17,7,9,15,2,9,4,6,2,15,4,7,15,12,10,0,7,7,3,12,19,4,11,14,15,0,13,17,3,12,2,16,12,5,2,5,0,14,1,19,9,1,1,10,13,7,15,1,19,15,19,14,2,7,9],[8,19,8,9,10,10,4,17,8,10,1,11,13,6,19,5,12,19,7,13,3,19,12,1,13,4,4,12,11,3,5,16,8,5,1,11,7,8,15,10,6,3,7,5,9,11,6,12,3,14,0,16,5,1,11,2,10,14,5],[9,7,13,5,11,5,17,15,5,7,16,15,12,18,11,0,1,13,15,2,2,2,8,16,7,1,16,12,11,18,6,11,10,5,17,13,8,7,7,19,1,12,19,2,1,9,2,11,16,14,7,10,6,12,7,0,1,6,16],[3,19,3,10,5,12,2,11,15,5,18,3,16,7,15,8,17,11,1,12,1,13,0,3,15,17,9,5,16,10,17,16,19,4,12,15,15,2,14,17,17,11,10,1,14,5,19,8,8,3,0,18,13,9,3,18,2,7,18],[4,7,3,18,5,10,12,2,5,14,4,16,10,5,18,15,17,14,17,18,14,7,17,12,2,15,14,14,17,5,0,9,14,1,15,17,6,6,8,19,4,1,14,6,4,16,2,2,3,16,14,17,11,6,16,3,5,8,6],[18,11,5,12,8,9,2,10,17,11,9,16,4,17,16,1,1,15,18,2,5,16,15,8,11,3,1,5,16,15,11,1,18,17,14,14,1,12,11,16,3,6,13,16,14,18,8,7,0,4,7,8,6,18,11,0,9,5,12],[16,8,17,9,13,11,6,12,7,14,18,8,14,2,8,7,3,16,0,5,3,16,11,7,14,12,10,4,5,7,13,2,9,1,1,9,12,13,3,14,19,18,13,7,15,11,0,16,14,10,0,1,18,4,11,13,12,7,8],[15,19,1,12,17,12,17,13,16,5,16,11,14,18,10,8,11,0,2,8,14,5,4,12,3,6,18,12,11,0,8,13,0,18,2,14,0,0,2,11,5,1,19,15,11,3,4,3,13,15,2,4,13,8,14,2,12,14,3],[3,11,12,13,7,2,19,15,14,10,18,18,13,0,10,3,6,8,19,1,14,11,2,13,13,7,3,13,4,6,2,19,8,19,1,16,19,1,13,7,17,4,10,8,1,2,6,19,5,11,2,10,1,9,16,8,2,3,13],[3,8,1,19,1,18,9,7,19,10,9,8,7,6,6,12,12,9,10,16,12,16,10,0,18,7,13,2,14,12,17,19,10,16,7,16,10,6,11,2,4,18,3,1,7,10,1,7,7,7,0,9,18,10,14,9,9,10,6],[2,13,15,4,13,9,14,3,5,2,19,7,17,2,1,7,19,18,4,15,9,19,5,13,16,16,19,19,4,19,17,17,14,1,14,18,3,16,0,2,2,4,18,14,2,18,17,8,2,5,1,0,8,10,13,4,5,7,16],[17,10,4,4,8,13,0,1,9,12,11,0,14,8,13,18,16,11,6,16,6,18,16,16,10,17,15,17,18,8,19,5,16,11,1,11,3,0,16,5,10,5,15,1,3,2,16,16,13,8,15,16,6,4,10,10,12,3,6],[8,14,10,3,9,4,7,4,11,13,6,9,14,14,19,6,14,15,4,15,7,4,3,10,3,3,3,8,10,14,16,3,9,1,10,9,4,17,17,15,2,6,3,13,15,11,15,6,13,17,7,8,8,11,18,2,3,17,3],[18,9,11,6,14,18,2,16,6,12,6,7,17,13,15,15,18,5,6,16,18,17,6,18,19,7,1,15,3,18,9,17,11,14,5,10,19,14,9,0,13,6,5,15,18,12,15,19,8,11,19,16,6,14,4,9,1,19,1],[12,13,19,6,19,4,2,6,12,15,1,11,19,12,17,12,14,6,11,18,18,1,14,1,4,14,0,7,4,18,12,1,9,1,17,5,19,5,1,7,15,17,19,3,8,11,14,17,11,2,18,9,4,7,17,18,8,14,6],[14,0,6,3,7,12,9,18,1,17,5,10,8,12,5,7,13,7,0,16,6,0,12,18,7,10,18,9,17,16,11,18,9,0,0,12,0,3,15,9,14,19,17,6,10,16,19,3,6,13,5,0,8,3,10,9,9,19,19],[17,9,12,7,3,13,0,8,10,16,3,17,2,17,15,0,16,14,0,7,10,14,5,10,9,15,9,19,8,6,3,2,6,4,3,0,17,1,0,0,18,3,16,5,1,10,5,12,16,3,11,5,12,16,11,16,1,3,9],[19,16,19,10,3,3,12,18,13,12,1,13,10,3,9,18,10,13,11,11,11,11,16,4,14,1,16,4,15,16,8,5,2,19,19,13,5,9,18,17,14,4,10,10,3,13,2,8,5,4,19,3,17,13,17,18,0,9,14],[2,11,7,10,7,13,14,14,7,1,4,10,19,19,17,15,8,14,18,14,11,12,9,11,16,3,9,5,6,1,8,3,7,19,17,17,0,6,10,17,8,3,15,1,18,1,3,5,9,8,3,17,7,9,12,13,10,9,0],[14,0,0,2,19,7,16,11,8,16,15,17,11,16,18,14,0,0,2,6,3,7,9,3,14,0,6,1,8,18,19,13,18,15,5,8,9,16,14,13,5,15,2,5,4,0,19,18,2,6,0,14,2,2,4,3,7,5,5],[8,13,11,16,13,15,8,5,4,1,0,12,14,10,16,9,4,17,19,13,15,2,0,2,0,1,16,19,2,10,16,9,9,11,17,17,7,7,0,10,15,3,6,7,15,0,4,12,4,2,17,13,8,0,9,12,14,16,7],[7,18,12,7,7,16,11,4,9,4,11,13,6,9,8,11,10,19,12,19,17,3,13,7,15,13,15,18,10,17,12,14,19,7,1,7,17,6,12,9,9,16,11,19,19,18,8,9,11,6,8,1,5,7,16,4,11,0,13],[11,16,9,8,5,5,2,18,11,16,1,10,19,9,10,3,8,15,3,18,2,11,18,13,2,17,3,3,16,8,10,12,16,3,15,12,13,9,10,17,1,17,10,14,7,17,6,3,10,5,14,10,9,1,16,7,9,12,10],[1,15,3,18,6,9,17,1,1,5,13,4,2,19,19,13,15,11,12,5,7,18,18,4,13,3,11,3,2,7,1,14,2,18,7,9,1,13,12,12,7,1,11,0,15,10,6,18,14,1,3,11,18,12,8,14,18,19,18],[0,10,0,19,2,7,12,13,14,6,14,7,17,12,17,11,9,3,2,10,10,14,12,1,19,18,3,5,2,1,17,5,2,6,16,3,2,7,6,17,2,9,2,6,18,15,2,7,11,14,2,15,11,19,17,7,13,1,13],[3,9,15,5,17,3,12,14,18,10,15,14,18,16,14,5,18,13,5,7,4,4,7,8,14,2,14,6,5,10,5,8,12,12,7,4,10,15,6,15,13,1,10,10,9,2,13,13,1,8,0,4,17,17,8,18,1,12,14],[9,6,18,4,1,7,5,14,9,4,7,14,19,14,9,4,17,10,16,17,8,1,13,8,14,4,16,11,1,2,15,8,10,19,8,7,4,8,10,3,8,14,2,2,18,3,1,6,16,8,11,4,7,8,10,7,12,10,5],[5,4,4,0,3,11,4,1,6,12,16,2,15,10,1,17,11,8,15,9,19,13,1,7,17,14,12,5,4,10,11,15,10,9,15,12,3,10,0,0,5,6,13,16,5,9,16,0,9,0,10,9,12,2,2,15,11,5,4],[0,4,4,2,11,10,18,19,9,9,14,0,10,12,2,5,6,1,15,8,18,4,7,17,17,4,6,18,15,12,3,9,9,1,7,4,16,5,12,9,13,4,8,4,10,15,16,3,10,16,4,5,3,0,13,6,17,4,6],[15,16,6,18,9,7,19,14,17,7,7,19,3,14,17,18,11,1,14,12,18,2,1,6,2,12,5,6,11,9,2,1,17,14,6,12,10,13,16,17,12,12,8,18,15,2,19,10,6,10,17,1,9,5,0,15,4,13,11],[17,2,13,13,7,6,12,16,17,2,5,11,1,18,13,12,3,12,12,6,15,5,10,7,1,5,7,6,3,11,1,17,12,9,5,2,12,8,19,0,19,19,18,1,1,2,17,14,8,18,15,11,2,19,13,2,5,19,7],[19,12,14,5,9,1,19,6,3,5,11,2,2,4,4,12,12,9,5,6,0,2,5,17,6,14,13,10,4,0,8,15,9,8,10,8,1,16,1,14,4,17,0,17,10,10,17,18,4,1,15,4,16,9,5,5,13,3,19],[17,2,17,11,18,18,14,15,7,11,7,13,10,8,0,19,14,16,2,15,2,8,14,4,4,0,8,19,6,6,1,0,3,4,15,15,4,14,10,0,5,1,0,1,14,3,7,0,3,11,3,12,19,16,11,15,10,6,0],[17,2,13,17,9,4,2,3,15,1,8,9,2,15,12,1,3,15,10,13,10,16,5,12,10,17,1,4,13,13,11,17,17,8,9,14,14,3,0,1,4,9,4,10,1,0,0,8,13,7,0,10,2,10,4,0,3,4,10],[6,9,10,11,11,10,0,14,8,2,13,6,17,7,12,11,9,8,1,17,13,18,15,1,10,0,16,17,4,5,0,11,16,8,12,2,10,8,14,13,8,2,1,0,2,1,18,0,6,4,2,7,6,18,16,14,6,15,15],[17,0,19,9,5,14,14,15,8,13,3,14,5,12,18,11,14,1,4,7,0,6,13,2,2,5,6,4,19,9,16,14,10,11,18,19,11,12,10,9,9,10,4,8,14,14,9,1,19,10,6,15,2,8,18,5,14,15,16],[0,2,15,14,6,1,11,12,18,6,11,17,7,12,10,5,15,5,18,0,16,9,17,18,6,18,16,12,0,14,12,14,1,18,12,19,11,10,10,19,6,5,18,19,8,3,16,4,13,14,2,2,19,2,10,16,16,17,19],[2,15,4,4,11,1,12,1,17,8,1,17,9,3,17,9,12,9,0,15,8,13,11,8,12,11,16,4,5,9,18,8,10,17,18,10,15,18,6,1,8,18,18,16,4,18,9,0,11,13,1,18,1,0,19,8,6,3,1],[16,19,5,10,19,5,18,2,7,6,19,11,8,9,12,8,2,19,9,9,0,16,10,4,18,19,16,3,0,8,5,10,2,12,9,4,12,6,11,2,15,8,15,16,3,4,14,9,1,18,5,6,16,17,0,6,11,15,10],[5,6,13,17,7,6,6,11,0,0,19,8,11,10,13,16,16,13,16,17,15,11,6,10,15,12,4,4,19,1,19,18,12,5,6,0,8,11,19,4,19,10,13,16,1,16,6,0,0,18,7,1,0,8,5,5,16,5,1],[13,7,7,9,4,13,17,10,1,9,11,2,8,1,6,7,12,16,16,6,5,15,10,18,5,2,2,5,16,2,2,6,12,10,6,4,16,3,11,6,17,10,16,12,4,4,1,19,15,0,3,1,18,5,10,12,6,2,8],[13,8,17,15,13,5,12,2,16,15,7,14,10,12,10,2,11,14,2,18,14,10,5,5,7,19,7,1,19,19,4,17,9,2,11,13,2,15,5,7,17,19,8,18,1,13,3,19,5,8,11,9,7,16,14,18,8,15,0],[16,4,1,4,10,5,16,15,14,3,10,4,17,15,13,10,7,16,13,7,1,0,12,10,10,8,10,17,17,0,12,13,9,10,12,2,10,5,12,6,12,14,12,19,11,6,13,18,14,7,3,16,6,14,3,19,10,6,19],[12,11,12,13,13,2,10,14,14,15,14,0,10,3,11,10,12,18,19,16,7,18,6,7,4,3,18,6,19,8,4,4,5,8,3,18,16,10,8,2,8,11,6,2,3,9,15,10,5,14,5,0,6,7,8,7,4,19,12],[15,9,16,11,5,6,4,8,3,7,10,15,18,16,15,3,8,19,0,18,12,8,11,8,11,14,18,17,0,16,13,5,4,5,10,11,13,0,19,13,8,5,16,4,2,13,7,8,4,13,15,16,15,10,16,12,3,11,6],[15,13,16,16,19,0,8,15,12,12,15,16,18,1,3,2,15,7,1,19,17,4,18,7,12,12,3,10,5,14,18,15,8,15,12,13,4,6,1,2,1,7,19,12,9,14,16,3,19,1,10,3,10,6,8,6,11,14,19],[13,15,10,11,5,0,9,8,4,11,0,5,0,18,14,8,15,11,11,4,0,17,7,9,18,7,13,3,14,1,10,18,5,3,9,8,17,9,4,8,13,7,13,11,11,5,0,14,5,17,16,4,4,9,18,2,9,1,12],[13,7,13,0,0,18,4,5,19,18,5,15,11,11,11,0,8,9,0,14,13,9,18,13,12,3,14,13,2,0,3,5,17,7,4,7,4,3,13,0,12,12,16,0,7,3,8,19,15,17,11,19,9,12,14,10,6,9,1],[4,3,10,9,3,8,16,17,3,8,13,12,0,18,4,12,7,14,5,11,5,4,4,16,15,9,2,13,7,8,3,6,5,9,1,19,2,4,9,17,17,11,0,13,7,5,12,3,9,10,17,9,2,8,1,1,0,11,11]]


后来我将DFS函数的参数中的二维lens前面加了’&’号,就AC了!真坑啊!!!!

原来就是这一个小地方,如果不加&号每次传入的是整一个数据,加了之后只是传入一个地址,减少了空间

贴下最后AC的代码

class Solution {
public:
int direct[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int dfs_search(int i,int j,int row,int col,vector<vector<int> >&visited,vector<vector<int> > &lens,vector< vector<int> > &matrix){
if(lens[i][j]) return lens[i][j];
int ans = 1;
for(int g = 0;g<4;g++){
int x = i + direct[g][0], y = j + direct[g][1];
if(x<row&&x>=0&&y<col&&y>=0 && !visited[x][y] && matrix[x][y]>matrix[i][j]){
visited[x][y] = 1;
ans = max(ans,dfs_search(x,y,row,col,visited,lens,matrix)+1);
visited[x][y] = 0;
}
}
lens[i][j] = ans;
return ans;
}
int longestIncreasingPath(vector<vector<int> >& matrix) {
int row = matrix.size();
if(row == 0) return 0;
int col = matrix[0].size();
vector<vector<int> > visited(row, vector<int>(col,0));
vector<vector<int> > lens(row, vector<int>(col,0));
int ans = -1;
for(int i=0;i<row;i++)
for(int j = 0;j<col;j++){
visited[i][j] = 1;
lens[i][j] = dfs_search(i,j,row,col,visited,lens,matrix);
visited[i][j] = 0;
ans = max(ans,lens[i][j]);
}
return ans;
}
};


这个做法是可以改进,不需要visited的,但是在dfs函数中需要将四个遍历写出来,类似的可以看一下下面的python代码

python

def longestIncreasingPath(self, matrix):
def dfs(i, j):
if not dp[i][j]:
val = matrix[i][j]
dp[i][j] = 1 + max(
dfs(i - 1, j) if i and val > matrix[i - 1][j] else 0,
dfs(i + 1, j) if i < M - 1 and val > matrix[i + 1][j] else 0,
dfs(i, j - 1) if j and val > matrix[i][j - 1] else 0,
dfs(i, j + 1) if j < N - 1 and val > matrix[i][j + 1] else 0)
return dp[i][j]

if not matrix or not matrix[0]: return 0
M, N = len(matrix), len(matrix[0])
dp = [[0] * N for i in range(M)]
return max(dfs(x, y) for x in range(M) for y in range(N))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息