Leetcode 329. Longest Increasing Path in a Matrix
2016-01-21 02:55
381 查看
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].
Analysis:
given the two-dimensional integer array, we need to find the Longest Increasing Path in a Matrix. we can move to four directions, updown right and left.
The first method is DFS, however it takes too much time, IF we use DFS, we need to start with all positions in this array and continue
for the whole
array.
code:
DFS, I did not check the correcness of this methodpublic class Solution {
int maxNum = Integer.MIN_VALUE;
public int longestIncreasingPath(int[][] matrix) {
int rowNum = matrix.length;
if(rowNum == 0)
return 0;
int columnNum = matrix[0].length;
if(column == 0)
return 0;
int row = 0;
int col = 0;
int number = 0;
for(int i = 0; i < rowNum;i++){
for(int j = 0; j < columnNum; j++){
helper(i,j,matirx,0)
}
}
return maxNum;
}
//This is the depth first search
public int helper(int row, int col, int[][] matrix,int number){
number++;
if(number > maxNum )
maxNum = number;
// if(number == 1)
// {
// lastOne = matrix[row, col];
int rowNum = matrix.length;
int colNum = matrix[0].length;
int temNum = number++;
if(row - 1 >=0 && matrix[row - 1][col] > matrix[row][col]){
int val = helper(row - 1,col,matrix,number);
if(val > maxNum)
maxNum = number;
if(val > temNum)
temNum = val;
}
if(row + 1 < rowNum && matrix[row + 1][col] > matrix[row][col]){
int val = helper(row + 1, col, matrix, number);
if(val > maxNum)
maxNum = number;
if(val > temNum)
temNum = val;
}
if(col - 1 >=0 && matrix[row][col - 1] > matrix[row][col]){
int val = helper(row, col - 1, matrix, number);
if(val > maxNum)
maxNum = number;
if(val > temNum)
temNum = val;
}
if(col + 1 >=colNum && matrix[row][col + 1] > matrix[row][col]){
int val = helper(row, col + 1, matrix,number);
if(val > maxNum)
maxNum = number;
if(val > temNum)
temNum = val;
}
return temNum;
}
}Dynamic programming + DFS.
public class Solution {
int maxNum = Integer.MIN_VALUE;
int[][] record;
public int longestIncreasingPath(int[][] matrix) {
int rowNum = matrix.length;
if(rowNum == 0)
return 0;
int colNum = matrix[0].length;
if(colNum == 0)
return 0;
record= new int[rowNum][colNum];
//initialized the record array to all 0;
for(int i = 0; i < rowNum; i++){
for(int j = 0; j < colNum; j++){
record[i][j] = 0;
}
}
int row = 0;
int col = 0;
for(int i = 0; i < rowNum;i++){
for(int j = 0; j < colNum; j++){
helper(i,j,matrix);
}
}
return maxNum;
}
//This is the depth first search
public int helper(int row, int col, int[][] matrix){
if(record[row][col] != 0)
return record[row][col];
// int number = 1;
// number++;
// if(number > maxNum )
// maxNum = number;
// if(number == 1)
// {
// lastOne = matrix[row, col];
int rowNum = matrix.length;
int colNum = matrix[0].length;
int temNum = 1;
if(row - 1 >=0 && matrix[row - 1][col] > matrix[row][col]){
int val = 1 + helper(row - 1,col,matrix);
if(val > maxNum)
maxNum = val;
if(val > temNum)
4000
temNum = val;
}
if(row + 1 < rowNum && matrix[row + 1][col] > matrix[row][col]){
int val = 1 + helper(row + 1, col, matrix);
if(val > maxNum)
maxNum = val;
if(val > temNum)
temNum = val;
}
if(col - 1 >=0 && matrix[row][col - 1] > matrix[row][col]){
int val = 1 + helper(row, col - 1, matrix, number);
if(val > maxNum)
maxNum = val;
if(val > temNum)
temNum = val;
}
if(col + 1 < colNum && matrix[row][col + 1] > matrix[row][col]){
int val = 1 + helper(row, col + 1, matrix,number);
if(val > maxNum)
maxNum = val;
if(val > temNum)
temNum = val;
}
if(temNum > maxNum )
maxNum = temNum;
record[row][col] = temNum;
return temNum;
// else{
}
}
Lesson learned
I learn from this program that when do the dynamic programming, you need to consider whether or not to store the computation result in a array, so that you can avoid repeated computation. This may be called purnning.
相关文章推荐
- C# delegate
- Java编码规范
- 小娜学法(9):什么是包庇?
- 微赞,你正在使用本地安装, 但未下载完整安装包, 请从微赞官网下载完整安装包后重试.
- 在成为一名it精英的路上,加油吧!
- 92b1 类的抽象理解
- LVM 命令集总结(转)
- 物理卷操作命令:pvcreate,pvscan,pvdisplay.卷组操作命令:vgcreate,vgdisplay. (转)
- DFS-----Route Between Two Nodes in Graph
- LVM pvcreate,vgcreate,lvcreate,mkfs
- Meteor - Android 打包
- c语言基本数据类型short、int、long、char、float、double
- 阿岳之_httpd2.4配置篇
- python类型转换、数值操作
- 初学Unity(无法响应的简单原因以及队列的使用)
- iOS实现图像指定区域模糊
- Linux 环境下使用 C API 连接 MYSQL 数据库
- TCP demo
- 迟来的2015年终总结
- DP -----Backpack