LeetCode 695 Max Area of Island

题目:

Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

Example 1:

[[0,0,1,0,0,0,0,1,0,0,0,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,1,1,0,1,0,0,0,0,0,0,0,0],
 [0,1,0,0,1,1,0,0,1,0,1,0,0],
 [0,1,0,0,1,1,0,0,1,1,1,0,0],
 [0,0,0,0,0,0,0,0,0,0,1,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,0,0,0,0,0,0,1,1,0,0,0,0]]

Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.

 

 

Example 2:

[[0,0,0,0,0,0,0,0]]

Given the above grid, return 0.

 

 

Note: The length of each dimension in the given grid does not exceed 50.

 

分析:

 

分析每一个元素的特点:

如果为0, 跳过;

如果为1,向上下左右分别搜索

 

初次提交:

class Solution {
    

    
    public int maxAreaOfIsland(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
   
        int maxArea = 0;
        //HashMap<Integer, Integer> checkedPnt = new HashMap<Integer, Integer>();
        
        ArrayList<Point> visitedPoint = new ArrayList<Point>();
        
        for(int i=0; i<m; i++) {
            for(int j=0; j<n; j++) {
                int currentArea = calculateArea(grid, i, j, visitedPoint);
                if(maxArea<currentArea) {
                    maxArea = currentArea;
                }
            }
        }
        
        return maxArea;
    }
    
    
    //ArrayList<Point> visitedPoint = new ArrayList<Point>();
    
    public int calculateArea(int[][] grid, int i, int j, ArrayList<Point> visitedPoint) {
           int m = grid.length;
           int n = grid[0].length;
        
        if( !((0<=i && i<m) && (0<= j && j<m)) ) {
            return 0;
        }
        
        if(grid[i][j] == 0) {
            
            return 0;
        }
        
        int area = 0;
        Point currentPoint = new Point(i,j);
        if(grid[i][j] == 1 && !visitedPoint.contains(currentPoint)) {
            area++;
            visitedPoint.add(currentPoint);
            
            area += calculateArea( grid, i,  j+1, visitedPoint);
            area += calculateArea( grid, i,  j-1, visitedPoint);
            area += calculateArea( grid, i+1,  j, visitedPoint);
            area += calculateArea( grid, i-1, j, visitedPoint);
            
            return area;
        } else {
            return area;
        }
        
        //return 0;
    }
}

    
    class Point {
        public  int x;
                int y;
        
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

 结果:

Your answer

Exception in thread "main" java.lang.StackOverflowError
    at java.util.ArrayList.contains(ArrayList.java:257)
    at Solution.calculateArea(Solution.java:44)
    at Solution.calculateArea(Solution.java:49)
    at Solution.calculateArea(Solution.java:48)
    at Solution.calculateArea(Solution.java:49)
    at Solution.calculateArea(Solution.java:48)
    at Solution.calculateArea(Solution.java:49)
    at Solution.calculateArea(Solution.java:48)
    at Solution.calculateArea(Solution.java:49)
    at Solution.calculateArea(Solution.java:48)
    at Solution.calculateArea(Solution.java:49)
    at Solution.calculateArea(Solution.java:48)
    at Solution.calculateArea(Solution.java:49)
    at Solution.calculateArea(Solution.java:48)
    at Solution.calculateArea(Solution.java:49)
    at Solution.calculateArea(Solution.java:48)
    at Solution.calculateArea(Solution.java:49)
    at Solution.calculateArea(Solution.java:48)
    at Solution.calculateArea(Solution.java:49)
    at Solution.calculateArea(Solution.java:48)
    at Solution.calculateArea(Solution.java:49)
    at Solution.calculateArea(Solution.java:48)
    at Solution.calculateArea(Solution.java:49)
    at Solution.calculateArea(Solution.java:48)
    at Solution.calculateArea(Solution.java:49)
    at Solution.calculateArea(Solution.java:48)
    at Solution.calculateArea(Solution.java:49)

 

分析:

 

o.equals(e)

 

需要重写Point的equals 方法。或许还有hashCode方法?

 

class Solution {
    

    
    public int maxAreaOfIsland(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
   
        int maxArea = 0;
        //HashMap<Integer, Integer> checkedPnt = new HashMap<Integer, Integer>();
        
        ArrayList<Point> visitedPoint = new ArrayList<Point>();
        
        for(int i=0; i<m; i++) {
            for(int j=0; j<n; j++) {
                int currentArea = calculateArea(grid, i, j, visitedPoint);
                if(maxArea<currentArea) {
                    maxArea = currentArea;
                }
            }
        }
        
        return maxArea;
    }
    
    
    //ArrayList<Point> visitedPoint = new ArrayList<Point>();
    
    public int calculateArea(int[][] grid, int i, int j, ArrayList<Point> visitedPoint) {
           int m = grid.length;
           int n = grid[0].length;
        
        if( !((0<=i && i<m) && (0<= j && j<m)) ) {
            return 0;
        }
        
        if(grid[i][j] == 0) {
            
            return 0; 
        }
        
        int area = 0;
        Point currentPoint = new Point(i,j);
        if(grid[i][j] == 1 && !visitedPoint.contains(currentPoint)) {
            area++;
            visitedPoint.add(currentPoint);
            
            area += calculateArea( grid, i,  j+1, visitedPoint);
            area += calculateArea( grid, i,  j-1, visitedPoint);
            area += calculateArea( grid, i+1,  j, visitedPoint);
            area += calculateArea( grid, i-1, j, visitedPoint);
            
            return area;
        } else {
            return area;
        }
        
        //return 0;
    }
}

    
    class Point {
        public  int x;
                int y;
        
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
        
        @Override
        public boolean equals(Point point) {
            if(this.x == point.x && this.y == point.y) {
                return true;
            } else {
                return false;
            }
        }
        
        @Override
        public int hashCode() {  
            return Objects.hash(x, y);  
        }  
    }

 

修复几个编译错误:

class Solution {
    

    
    public int maxAreaOfIsland(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
   
        int maxArea = 0;
        //HashMap<Integer, Integer> checkedPnt = new HashMap<Integer, Integer>();
        
        ArrayList<Point> visitedPoint = new ArrayList<Point>();
        
        for(int i=0; i<m; i++) {
            for(int j=0; j<n; j++) {
                int currentArea = calculateArea(grid, i, j, visitedPoint);
                if(maxArea<currentArea) {
                    maxArea = currentArea;
                }
            }
        }
        
        return maxArea;
    }
    
    
    //ArrayList<Point> visitedPoint = new ArrayList<Point>();
    
    public int calculateArea(int[][] grid, int i, int j, ArrayList<Point> visitedPoint) {
           int m = grid.length;
           int n = grid[0].length;
        
        if( !((0<=i && i<m) && (0<= j && j<m)) ) {
            return 0;
        }
        
        if(grid[i][j] == 0) {
            
            return 0; 
        }
        
        int area = 0;
        Point currentPoint = new Point(i,j);
        if(grid[i][j] == 1 && !visitedPoint.contains(currentPoint)) {
            area++;
            visitedPoint.add(currentPoint);
            
            area += calculateArea( grid, i,  j+1, visitedPoint);
            area += calculateArea( grid, i,  j-1, visitedPoint);
            area += calculateArea( grid, i+1,  j, visitedPoint);
            area += calculateArea( grid, i-1, j, visitedPoint);
            
            return area;
        } else {
            return area;
        }
        
        //return 0;
    }
}

    
    class Point {
        public  int x;
                int y;
        
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
        
        @Override
        public boolean equals(Object object) {
            if(object instanceof Point) {
                Point point = (Point) object;
                return (this.x == point.x && this.y == point.y);
            } else {
                return super.equals(object);
            }
        }
        
        @Override
        public int hashCode() {  
            return Objects.hash(x, y);  
        }  
    }

 

结果:

Submission Result: Runtime Error 
Runtime Error Message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at Solution.calculateArea(Solution.java:37)
    at Solution.calculateArea(Solution.java:48)
    at Solution.maxAreaOfIsland(Solution.java:16)
    at __DriverSolution__.__helper__(__Driver__.java:8)
    at __Driver__.main(__Driver__.java:52)
Last executed input: [[0],[1]]

 

分析:又是数组越界.

 

 

再次提交:

class Solution {
    

    
    public int maxAreaOfIsland(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
   
        int maxArea = 0;
        //HashMap<Integer, Integer> checkedPnt = new HashMap<Integer, Integer>();
        
        ArrayList<Point> visitedPoint = new ArrayList<Point>();
        
        for(int i=0; i<m; i++) {
            for(int j=0; j<n; j++) {
                int currentArea = calculateArea(grid, i, j, visitedPoint);
                if(maxArea<currentArea) {
                    maxArea = currentArea;
                }
            }
        }
        
        return maxArea;
    }
    
    
    //ArrayList<Point> visitedPoint = new ArrayList<Point>();
    
    public int calculateArea(int[][] grid, int i, int j, ArrayList<Point> visitedPoint) {
           int m = grid.length;
           int n = grid[0].length;
        
        if( !((0<=i && i<m) && (0<= j && j<n)) ) {
            return 0;
        }
        
        if(grid[i][j] == 0) {
            
            return 0; 
        }
        
        int area = 0;
        Point currentPoint = new Point(i,j);
        if(grid[i][j] == 1 && !visitedPoint.contains(currentPoint)) {
            area++;
            visitedPoint.add(currentPoint);
            
            area += calculateArea( grid, i,  j+1, visitedPoint);
            area += calculateArea( grid, i,  j-1, visitedPoint);
            area += calculateArea( grid, i+1,  j, visitedPoint);
            area += calculateArea( grid, i-1, j, visitedPoint);
            
            return area;
        } else {
            return area;
        }
        
        //return 0;
    }
}

    
    class Point {
        public  int x;
                int y;
        
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
        
        @Override
        public boolean equals(Object object) {
            if(object instanceof Point) {
                Point point = (Point) object;
                return (this.x == point.x && this.y == point.y);
            } else {
                return super.equals(object);
            }
        }
        
        @Override
        public int hashCode() {  
            return Objects.hash(x, y);  
        }  
    }

 

结果:

这一结果显然时间复杂度很高。 猜测是引入了新类Point引起的。

解决办法,看了一下这道题的solution, 可以使用一个boolean [][] seen来记录是否被访问过。因为岛的大小不大,不会占用太多空间, 而时间上数组比ArrayList效率高。

还有一点需要注意,就是可以把递归转化为迭代。 但是在本题中并不会减少时间复杂度。

 

 

修改后提交:

class Solution {
    

    
    public int maxAreaOfIsland(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
   
        int maxArea = 0;
        
        int[][] seen = new int[m][n];
        
        for(int i=0; i<m; i++) {
            for(int j=0; j<n; j++) {
                int currentArea = calculateArea(grid, i, j, seen);
                if(maxArea<currentArea) {
                    maxArea = currentArea;
                }
            }
        }
        
        return maxArea;
    }
    
    
    //ArrayList<Point> visitedPoint = new ArrayList<Point>();
    
    public int calculateArea(int[][] grid, int i, int j, int[][] seen) {
           int m = grid.length;
           int n = grid[0].length;
        
        if( !((0<=i && i<m) && (0<= j && j<n)) ) {
            return 0;
        }
        
        if(grid[i][j] == 0) {
            return 0; 
        }
        
        int area = 0;
        //Point currentPoint = new Point(i,j);
        if(grid[i][j] == 1 && !(seen[i][j] == 1)) {
            area++;
            seen[i][j] = 1;
            
            area += calculateArea( grid, i,  j+1, seen);
            area += calculateArea( grid, i,  j-1, seen);
            area += calculateArea( grid, i+1,  j, seen);
            area += calculateArea( grid, i-1, j, seen);
            
            return area;
        } else {
            return area;
        }
        
        //return 0;
    }
}

 

结果:

 

 

修改后提交:

class Solution {
    

    
    public int maxAreaOfIsland(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
   
        int maxArea = 0;
        
        int[][] seen = new int[m][n];
        
        for(int i=0; i<m; i++) {
            for(int j=0; j<n; j++) {
                int currentArea = calculateArea(grid, i, j, seen);
                if(maxArea<currentArea) {
                    maxArea = currentArea;
                }
            }
        }
        
        return maxArea;
    }
    
    
    //ArrayList<Point> visitedPoint = new ArrayList<Point>();
    
    public int calculateArea(int[][] grid, int i, int j, int[][] seen) {
           int m = grid.length;
           int n = grid[0].length;
        
        if( !((0<=i && i<m) && (0<= j && j<n)) ) {
            return 0;
        }
        
        if(grid[i][j] == 0) {
            return 0; 
        }
        
        int area = 0;
        //Point currentPoint = new Point(i,j);
        if(grid[i][j] == 1 && !(seen[i][j] == 1)) {
            area++;
            seen[i][j] = 1;
            
            /*area += calculateArea( grid, i,  j+1, seen);
            area += calculateArea( grid, i,  j-1, seen);
            area += calculateArea( grid, i+1,  j, seen);
            area += calculateArea( grid, i-1, j, seen);*/
            
            return area + calculateArea( grid, i,  j+1, seen) + calculateArea( grid, i,  j-1, seen) + 
                calculateArea( grid, i+1,  j, seen) + calculateArea( grid, i-1, j, seen);
            
            //return area;
        } else {
            return area;
        }
        
        //return 0;
    }
}

 

结果:

 

总结:

这道题花了不少时间。 主要是一开始忘记了递归这一利器。

为什么是深度优先遍历呢?

posted @ 2018-04-17 20:11  Zhao_Gang  阅读(133)  评论(0编辑  收藏  举报