1. 程式人生 > >LeetCode 695 Max Area of Island

LeetCode 695 Max Area of Island

ges return 題目 向上 assume because code HR 一個

題目:

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引起的。

總結:

這道題花了不少時間。 主要是一開始忘記了遞歸這一利器。

LeetCode 695 Max Area of Island