Cleanest and Shortest answer with inline explanation. Upvote me!

  • 2
    public List<int[]> pacificAtlantic(int[][] matrix) {
        List<int[]> result=new ArrayList<int[]>();
        if(matrix.length==0||matrix[0].length==0) return result;
        int height=matrix.length,width=matrix[0].length;
        boolean [][]Pacific=new boolean[height][width],Atlantic=new boolean[height][width];
        for(int i=0;i<height;i++) {
            recursiveMark(Pacific,matrix,i,0,0); //We start from left edge of Pacific
            recursiveMark(Atlantic,matrix,i,width-1,0); //We start from right edge of Atlantic
        for(int j=0;j<width;j++) {
            recursiveMark(Pacific,matrix,0,j,0); //We start from top edge of Pacific
            recursiveMark(Atlantic,matrix,height-1,j,0); //We start from bot edge of Atlantic
        for(int i=0;i<height;i++)
          for(int j=0;j<width;j++){
              if(Pacific[i][j]&&Atlantic[i][j]) {
                  int []newOne={i,j};
        return result;
    public void recursiveMark(boolean [][]map,int[][]matrix,int i,int j,int pre){ 
        if(i<0||j<0||i>=map.length||j>=map[0].length) return;
        if(map[i][j]==true) return; //When this is visited before, we quit.
        int current=matrix[i][j];
        if(current>=pre) map[i][j]=true;
        else return;

  • 0

    I didn't read your code, I only downvoted because your title commands/begs me to upvote.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.