亚马逊面试 [leetcode] 329. Longest Increasing Path in a Matrix

2017-06-19 亚马逊面试过程:


第一轮,给一个情景让你设计一个接口,然后对接口里面用到的类要自己实现iterator里的应用;

第二轮leetcode329;

第三轮自己设计一个音乐播放器,主要看你怎么写shuffle的算法;

第四轮hiring manager会问一些你以前project的问题在这四轮中一直穿插着一些amazon leadership principle的问题问你。


leetcode329 题目和答案如下:

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.

这道题是求矩阵中最长递增路径长度,题目难度为Hard。分析:这道题很容易想到DFS+动态规划,另外未来打印出最长递增路径的所有节点来,得设计与数组大小一样的另外的一个数组,该数值的值要保留该节点的下一个节点的坐标,这样,知道了最长递增路径的最小数值的那个节点的坐标,就可以顺藤摸瓜地,打印出来整个路径上的节点了。代码如下:

  • DFS+动态规划 
  • package algorithm;
    public class maze_longarray {
    	int[][] matrix;
    	int[][] countMatrix; // countMatrix[row][column]存放的是以matrix[row][column]为起点的最长递增数列长度
    	int matrixRow;
    	int matrixCol;
    	int MaxArrayLen = 0; // 保存最大的递增数组长度
    	// 存储最大的递增数组链上的节点信息和它的下一节点信息
    
    	class internalNode {
    		public int x, y;
    		public internalNode nextNode;
    
    		internalNode(int x1, int y1) {
    			x = x1;
    			y = y1;
    			nextNode = null;
    		};
    internalNode() {
    			x = 0;
    			y = 0;
    			nextNode = null;
    		}
    	}
    
    	internalNode[][] historylink = null;
    	internalNode HeadNode = null;
    public static void mazeLongestArrayShow() {
    		int[][] matrix = { { 9, 9, 4 }, { 6, 6, 8 }, { 2, 1, 1 } };
    		int[][] matrix1 = { { 3, 4, 5 }, { 3, 2, 6 }, { 2, 2, 1 } };
    
    		maze_longarray maze = new maze_longarray();
    		int result = maze.longestIncreasingPath(matrix1);
    		System.out.println("the result longest array length:" + result);
    		return;
    	}
    
    	public int longestIncreasingPath(int[][] matrix) {
    		this.matrix = matrix;
    		matrixRow = matrix.length;
    		if (matrixRow <= 0)
    			return 0;
    		matrixCol = matrix[0].length;
    		countMatrix = new int[matrixRow][matrixCol];
    		historylink = new internalNode[matrixRow][matrixCol];for (int i = 0; i < matrixRow; i++) {
    			for (int j = 0; j < matrixCol; j++) {
    				historylink[i][j] = new internalNode();
    				historylink[i][j].x = i;
    				historylink[i][j].y = j;
    				historylink[i][j].nextNode = null;
    			}
    		}
    
    		initCountMatrix();
    
    		for (int i = 0; i < matrixRow; i++) {
    			for (int j = 0; j < matrixCol; j++) {
    				dfs(i, j);
    			}
    		}if (HeadNode != null) {
    			internalNode p = HeadNode;
    			while (p != null) {
    				System.out.print("(" + p.x + "," + p.y + ")->");
    				p = p.nextNode;
    			}
    		}
    		return MaxArrayLen;
    	}
    // dfs是求以节点(row,col)为数值的最长递增数组的长度,并放在 countMatrix(row,col)中。
    	private void dfs(int row, int col) {
    		int[] rows = { -1, 1, 0, 0 };
    		int[] cols = { 0, 0, -1, 1 };
    		int count = 1;
    		for (int i = 0; i < 4; i++) {
    			int tmpRow = row + rows[i];
    			int tmpCol = col + cols[i];
    			if (tmpRow < 0 || tmpRow >= matrixRow)
    				continue;
    			if (tmpCol < 0 || tmpCol >= matrixCol)
    				continue;
    			if (matrix[tmpRow][tmpCol] > matrix[row][col]) {
    				if (countMatrix[tmpRow][tmpCol] == -1) {
    					dfs(tmpRow, tmpCol);
    				}
    
    				if (countMatrix[tmpRow][tmpCol] + 1 > count) {
    					historylink[row][col].nextNode = historylink[tmpRow][tmpCol];
    				}
    				count = Math.max(count, countMatrix[tmpRow][tmpCol] + 1);
    			}
    		}countMatrix[row][col] = count;
    		if (count > MaxArrayLen)
    			//让HeadNode总是指向目前递增数列的头
    			HeadNode = historylink[row][col];
    		MaxArrayLen = Math.max(MaxArrayLen, count);
    	}
    
    	private void initCountMatrix() {
    		for (int i = 0; i < matrixRow; i++) {
    			for (int j = 0; j < matrixCol; j++)
    				countMatrix[i][j] = -1;
    		}
    	}
    }
    输出:
  • (0,0)->(0,1)->(0,2)->(1,2)->the result longest array length:4
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值