417. Pacific Atlantic Water Flow

Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges.

Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.

Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.

Note:

  1. The order of returned grid coordinates does not matter.
  2. Both m and n are less than 150.

Example:

Given the following 5x5 matrix:

  Pacific ~   ~   ~   ~   ~ 
       ~  1   2   2   3  (5) *
       ~  3   2   3  (4) (4) *
       ~  2   4  (5)  3   1  *
       ~ (6) (7)  1   4   5  *
       ~ (5)  1   1   2   4  *
          *   *   *   *   * Atlantic

Return:

[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).

思路:BFS或者DFS,目测DFS慢一点

package l417;


import java.util.ArrayList;
import java.util.List;

// 标准DFS + 2个全局boolean
public class dfs_solution {
	
	List<int[]> rst = new ArrayList<int[]>();
	int[] dx = new int[]{0,0,1,-1};
	int[] dy = new int[]{1,-1,0,0};
	boolean[][] marked, canReach;
	boolean Pacific = false, Atlantic = false;
	
    public List<int[]> pacificAtlantic(int[][] matrix) {
    	if(matrix.length == 0)	return rst;
    	int rows = matrix.length, cols = matrix[0].length;
    	marked = new boolean[rows][cols]; canReach = new boolean[rows][cols];
        
        for(int i=0; i<rows; i++)
        	for(int j=0; j<cols; j++) {
        		Pacific = false; Atlantic = false;
    			
    			marked[i][j] = true;
    			dfs(matrix, i, j);
    			marked[i][j] = false;
    			
    			if(Pacific && Atlantic)	{
    				canReach[i][j] = true;
    				rst.add(new int[]{i, j});
    			}
        	}
        
        return rst;
    }

	private void dfs(int[][] matrix, int i, int j) {
		if(i == 0 || j == 0)								Pacific = true;
		if(i == matrix.length-1 || j == matrix[0].length-1)	Atlantic = true;
		if(canReach[i][j])	{Pacific=true; Atlantic=true;}
		if(Pacific && Atlantic)	 return;
		
		for(int k=0; k<4; k++) {
			int newi = i+dx[k], newj = j+dy[k];
			
			if(ok(matrix, newi, newj) && !marked[newi][newj] && matrix[i][j]>=matrix[newi][newj]) {
				marked[newi][newj] = true;
				dfs(matrix, newi, newj);
				marked[newi][newj] = false;
			}
		}
	}

	private boolean ok(int[][] matrix, int i, int j) {
		if(i<0 || i>=matrix.length || j<0 || j>=matrix[0].length)	return false;
		return true;
	}

}




package l417;

import java.util.*;

public class Solution {
    public List<int[]> pacificAtlantic(int[][] matrix) {
    	List<int[]> rst = new ArrayList<int[]>();
    	if(matrix.length == 0)	return rst;
    	int rows = matrix.length, cols = matrix[0].length;
    	boolean[][] Pacific = new boolean[rows][cols], Atlantic = new boolean[rows][cols];
    	int[] dx = new int[]{0,0,1,-1};
    	int[] dy = new int[]{1,-1,0,0};
    	
    	Queue<int[]> cur = new LinkedList<int[]>();
    	Queue<int[]> next = new LinkedList<int[]>();
    	
    	// pacific
    	for(int i=0; i<rows; i++) {
    		Pacific[i][0] = true;
    		cur.add(new int[]{i, 0});
    	}
    	for(int i=0; i<cols; i++) {
    		Pacific[0][i] = true;
    		cur.add(new int[]{0, i});
    	}
    	while(!cur.isEmpty()) {
    		int[] r = cur.remove();
    		
    		for(int k=0; k<4; k++) {
    			int newi = r[0]+dx[k], newj = r[1]+dy[k];
    			if(ok(matrix, newi, newj) && !Pacific[newi][newj] && matrix[r[0]][r[1]]<=matrix[newi][newj])	{
    				next.add(new int[]{newi, newj});
    				Pacific[newi][newj] = true;
    			}
    		}
    		if(cur.isEmpty()) {
    			cur = next;
    			next = new LinkedList<int[]>();
    		}
    	}
    	
    	
    	// atlantic
    	for(int i=0; i<rows; i++) {
    		Atlantic[i][cols-1] = true;
    		cur.add(new int[]{i, cols-1});
    	}
    	for(int i=0; i<cols; i++) {
    		Atlantic[rows-1][i] = true;
    		cur.add(new int[]{rows-1, i});
    	}
    	while(!cur.isEmpty()) {
    		int[] r = cur.remove();
    		
    		for(int k=0; k<4; k++) {
    			int newi = r[0]+dx[k], newj = r[1]+dy[k];
    			if(ok(matrix, newi, newj) && !Atlantic[newi][newj] && matrix[r[0]][r[1]]<=matrix[newi][newj])	{
    				next.add(new int[]{newi, newj});
    				Atlantic[newi][newj] = true;
    			}
    		}
    		if(cur.isEmpty()) {
    			cur = next;
    			next = new LinkedList<int[]>();
    		}
    	}
    	
    	
    	for(int i=0; i<rows;i++)
    		for(int j=0; j<cols; j++)
    			if(Pacific[i][j] && Atlantic[i][j])
    				rst.add(new int[]{i, j});
    	return rst;
    }
    
    private boolean ok(int[][] matrix, int i, int j) {
		if(i<0 || i>=matrix.length || j<0 || j>=matrix[0].length)	return false;
		return true;
	}
}



但是为什么BFS还只击败了16%??还有哪里可以优化??

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值