每日一練——矩陣中的最大遞增路徑
題目描述:
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] ]
Return4
The longest increasing path is[1, 2, 6, 9]
.
Example 2:
nums = [ [3,4,5], [3,2,6], [2,2,1] ]
Return4
The longest increasing path is[3, 4, 5, 6]
. Moving diagonally is not allowed.
對於矩陣中的每一個數字,用深度優先遍歷尋找最比當前數字大的數字,對於已經走過的數字,將其置位為INT_MIN,下次經過這個數字可以直接不用考慮。如果走到某個位置,其上下左右的數字都比當前數字小,那麼說明已經無路可走,那麼就得進行回溯,回溯的過程中必須將之前置位為INT_MIN的數字恢復為初始數字。
程式碼如下:
class Solution { private: int curLen; int maxLen; int row; int col; public: void Move(vector<vector<int> >& matrix, int x, int y) { int cur = matrix[x][y]; matrix[x][y] = INT_MIN; curLen++; if (curLen > maxLen) { if (curLen == 8) { curLen = curLen; } maxLen = curLen; } if (y + 1 < col && matrix[x][y+1] > cur) { Move(matrix, x, y+1); } if (y - 1 >= 0 && matrix[x][y-1] > cur) { Move(matrix, x, y-1); } if (x + 1 < row && matrix[x+1][y] > cur) { Move(matrix, x+1, y); } if (x - 1 >= 0 && matrix[x-1][y] > cur) { Move(matrix, x-1, y); } matrix[x][y] = cur; curLen--; } int longestIncreasingPath(vector<vector<int> >& matrix) { if (matrix.empty()||matrix[0].empty()) return 0; row = matrix.size(); col = matrix[0].size(); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { curLen = 0; Move(matrix, i, j); } } return maxLen; } };
相關推薦
每日一練——矩陣中的最大遞增路徑
題目描述: Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: l
求一個數組中最大值與最小值
div return urn 技術 code include 一個 最小值 array #include <stdio.h> int main() { int array[10]={100,1,40,29,45,22,98,2,83,75};
(LeetCode每日一刷13)最大自序和
題目描述: 給定一個整數陣列 nums ,找到一個具有最大和的連續子陣列(子陣列最少包含一個元素),返回其最大和。 示例: 示例: 輸入: [-2,1,-3,4,-1,2,1,-5,4], 輸出: 6 解釋: 連續子陣列 [4
6-2 找出矩陣中最大值所在的位置
將1個3*2的矩陣存入1個3*2的二維陣列中,找出最大值以及它的行下標和列下標,並輸出該矩陣 #include<stdio.h> int main(void) {int col, i, j, row;int a[3][2];//將輸入的數存入二維陣列 for(i = 0
求一個數組中最大值函式的下標值
#include <stdio.h> int maxElement(array[] ,int n) { int i=0,n=0,t=array[0];//將下標為0的值賦值給t for(i=0;i<n;i++)//控制迴圈 { if(t<ar
微軟演算法100道題------求一個矩陣中最大的二維矩陣(元素和最大)
題目: 求一個矩陣中最大的二維矩陣(元素和最大).如: 1 2 0 3 4 2 3 4 5 1 1 1 5 3 0 中最大的是: 4 5 5 3 要求:(1)寫出演算法;(2)分析時間複雜度; 思路
【再回首Python之美】【矩陣】求矩陣中最大元素/最小元素的行列座標 For 層次聚類演算法Hierarchical Clustering Alg
求多維矩陣中最小元素的行列座標,這個在層次聚類演算法中用到,這裡實現記錄一下。1.簡介矩陣M: [[1 3 2] [2 6 0] [9 8 5]]最大元素是9,對應的行列座標為(2,0)最小元素是
取一個數組中最大和最小值
/*find minivalue*/minvalue=ADC_ConvertedValue[0];for(k=1;k<10;k++){ if(ADC_ConvertedValue[k]<minvalue) { minvalue=ADC_Converted
【DP】在矩陣中,選擇一條從左上角到右下角、經過數字之和最大的路徑
題目:EPI int max_fishing(vector<vector<int>> A) { if (A.empty() || A[0].empty()) throw new exception; for (int i = 0; i &l
【十一】找出一組數組中最大值、最大值的角標、最小值、最小值的角標及平均數
思路 原來 比較 快捷 兩個 span div 選擇排序 分數 需求:現有一組評委打分的數據。 求出他們的最大值與最小值。 最大值最小值的角標 去除最大值與最小值後的平均數 代碼: 1 <?php 2 $arr=array(1,2,3,4); 3
判斷一個數中最大回文數的長度
i++ length 偶數 判斷 brush while clas stat light 判斷一個數中最大回文數的長度 :例如12332112345654321中最大的回文數是12345654321,長度為11 public static void palindrome
329 Longest Increasing Path in a Matrix 矩陣中的最長遞增路徑
can you 遞增 c++ direct log integer ret pre Given an integer matrix, find the length of the longest increasing path.From each cell, you can
Hadoop_26_MapReduce_Reduce端使用GroupingComparator求同一訂單中最大金額的訂單
size 流程 機制 apach ble lose alt inf ping 1. 自定義GroupingComparator 1.1.需求:有如下訂單 現在需要求出每一個訂單中成交金額最大的一筆交易 1.2.分析: 1、利用“訂單id和成交金額”Bean作為key,
每日模板一練——矩陣快速冪
打個模板防止手生了吧。。。 大意:求Febnaci第n項(n<=1e9) #include<iostream> #include<cstdio> #include<cstring> #include<algorithm>
Java中獲取一個數組的最大值和最小值
先定義一個數組,獲取其中的最大值和最小值 package Hello; public class test { public static void main(String[] args) {
求矩陣的最大值(設惟一)
7-2 求矩陣的最大值(設惟一) (10 分) 本題要求編寫程式,求一個給定的m×n矩陣的最大值以及位置。題目保證最大值惟一。 輸入格式: 輸入第一行給出兩個正整數m和n(1≤m,n≤6)。隨後m行,每行給出n個整數,其間以空格分隔。 輸出格式: 輸出在第一行中輸出最大值,在第二行中
leetcode 矩陣中的最長遞增路徑 python【動態規劃】
題目描述 **分析:**假設最長路徑終點的是[i][j],則其最長路徑值為nums1[i][j],則nums1[i][j]等於它上下左右四個數中,比它小的數中最長路徑值最大的那一個+1 因此,我們可以從矩陣的最小值出發,其最長路徑值為1,然後計算第二小的數的最長路徑值,以此類推 cla
[LeetCode] 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
(LeetCode 329)矩陣中的最長遞增路徑 [簡單DP & 公式:dp[x][y] = dp[xx][yy] + 1]
329. 矩陣中的最長遞增路徑 給定一個整數矩陣,找出最長遞增路徑的長度。 對於每個單元格,你可以往上,下,左,右四個方向移動。 你不能在對角線方向上移動或移動到邊界外(即不允許環繞)。 示例 1: 輸入: nums = [ [9,9,4], [6,6,8], [2,1,1]
分治練手之求陣列中最大元素
程式碼: #include<stdio.h> int Max_Element(int *a,int s,int e){ int mid=(s+e)/2; int max,m1,m2;