<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)329:矩陣中的最長遞增路徑

          共 3632字,需瀏覽 8分鐘

           ·

          2021-07-27 01:05

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 矩陣中的最長遞增路徑,我們先來看題面:
          https://leetcode-cn.com/problems/longest-increasing-path-in-a-matrix/

          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).

          給定一個 m x n 整數(shù)矩陣 matrix ,找出其中 最長遞增路徑 的長度。

          對于每個單元格,你可以往上,下,左,右四個方向移動。你 不能 在 對角線 方向上移動或移動到 邊界外(即不允許環(huán)繞)。

          示例


          解題



          class Solution {
              public int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
              public int rows, columns;

              public int longestIncreasingPath(int[][] matrix) {
                  if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
                      return 0;
                  }
                  rows = matrix.length;
                  columns = matrix[0].length;
                  int[][] memo = new int[rows][columns];
                  int ans = 0;
                  for (int i = 0; i < rows; ++i) {
                      for (int j = 0; j < columns; ++j) {
                          ans = Math.max(ans, dfs(matrix, i, j, memo));
                      }
                  }
                  return ans;
              }

              public int dfs(int[][] matrix, int row, int column, int[][] memo) {
                  if (memo[row][column] != 0) {
                      return memo[row][column];
                  }
                  ++memo[row][column];
                  for (int[] dir : dirs) {
                      int newRow = row + dir[0], newColumn = column + dir[1];
                      if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && matrix[newRow][newColumn] > matrix[row][column]) {
                          memo[row][column] = Math.max(memo[row][column], dfs(matrix, newRow, newColumn, memo) + 1);
                      }
                  }
                  return memo[row][column];
              }
          }


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-320題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)321:拼接最大數(shù)

          LeetCode刷題實戰(zhàn)322:零錢兌換

          LeetCode刷題實戰(zhàn)323:無向圖中連通分量的數(shù)目

          LeetCode刷題實戰(zhàn)324:擺動排序 II

          LeetCode刷題實戰(zhàn)325:和等于 k 的最長子數(shù)組長度

          LeetCode刷題實戰(zhàn)326:3的冪
          LeetCode刷題實戰(zhàn)327:區(qū)間和的個數(shù)
          LeetCode刷題實戰(zhàn)328:奇偶鏈表

          瀏覽 19
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲无码视频观看 | 波多野结av衣东京热无码专区 | 麻豆激情四射在线播放 | 黄色欧美视频网站 | 黄色电影特黄一级片 |