国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

LeetCode[329] Longest Increasing Path in a Matrix

cppowboy / 936人閱讀

摘要:復雜度思路為了避免搜索已經搜索的點。所以考慮用一個數組,記錄到每一個點能產生的最長序列的長度。考慮用進行搜索,對于每一個點來說,考慮先找到最小的那個點針對每一條路徑的。然后對于每一點再遞增回去,依次累積找到增加的值。

LeetCode[329] Longest Increasing Path in a Matrix

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.

DP + DFS

復雜度
O(MN),O(N)

思路
為了避免搜索已經搜索的點。所以考慮用一個dp數組,記錄到每一個點能產生的最長序列的長度。考慮用dfs進行搜索,對于每一個點來說,考慮先找到最小的那個點針對每一條路徑的。然后對于每一點再遞增回去,依次累積找到增加的值。

代碼

public int longestIncreasingPath(int[][] matrix) {
    if(matrix.length == 0) return 0; 
    int row = matrix.length, col = matrix[0].length;
    int[][] dp = new int[row][col];
    //dp = {0};
    int val = 0;
    for(int i = 0; i < row; i ++) {
        for(int j = 0; j < col; j ++) {
            if(helper(matrix, i, j, dp) > val) {
                val = helper(matrix, i, j, dp);
            }
        }
    }
    return val;
}

public int helper(int[][] matrix, int i, int j, int[][] dp) {
    //this is the point;
    if(dp[i][j] != 0) return dp[i][j];
    int val = 0;
    if(i + 1 < matrix.length && matrix[i + 1][j] < matrix[i][j]) {
        val = Math.max(val, helper(matrix, i + 1, j, dp));
    }
    if(i - 1 >= 0 && matrix[i - 1][j] < matrix[i][j]) {
        val = Math.max(val, helper(matrix, i - 1, j, dp));
    }
    if(j + 1 < matrix[0].length && matrix[i][j + 1] < matrix[i][j]) {
        val = Math.max(val, helper(matrix, i, j + 1, dp));
    }
    if(j - 1 >= 0 && matrix[i][j - 1] < matrix[i][j]) {
        val = Math.max(val, helper(matrix, i, j - 1, dp));
    }
    dp[i][j] = val + 1;
    return dp[i][j];
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65237.html

相關文章

  • leetcode 329. Longest Increasing Path in a Matrix

    摘要:題目要求思路和代碼這里采用廣度優先算法加上緩存的方式來實現。我們可以看到,以一個節點作為開始構成的最長路徑長度是確定的。因此我們可以充分利用之前得到的結論來減少重復遍歷的次數。 題目要求 Given an integer matrix, find the length of the longest increasing path. From each cell, you can ei...

    heartFollower 評論0 收藏0
  • [LeetCode] 329. Longest Increasing Path in a Matri

    Problem 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 ou...

    hss01248 評論0 收藏0
  • [LeetCode] 329. Longest Increasing Path in a Matri

    Problem 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 ou...

    antz 評論0 收藏0
  • LeetCode 329. Longest Increasing Path in a Matrix

    摘要:思路這道題主要使用記憶化遞歸和深度優先遍歷。我們以給定的矩陣的每一個位置為起點,進行深度優先遍歷。我們存儲每個位置深度優先遍歷的結果,當下一次走到這個位置的時候,我們直接返回當前位置記錄的值,這樣可以減少遍歷的次數,加快執行速度。 Description Given an integer matrix, find the length of the longest increasing...

    isLishude 評論0 收藏0
  • 329. Longest Increasing Path in a Matrix

    摘要:題目解答最重要的是用保存每個掃過結點的最大路徑。我開始做的時候,用全局變量記錄的沒有返回值,這樣很容易出錯,因為任何一個用到的環節都有可能改變值,所以還是在函數中定義,把當前的直接返回計算不容易出錯。 題目:Given an integer matrix, find the length of the longest increasing path. From each cell, y...

    hqman 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<