LeetCode 695 Max Area of Island
Posted hzg1981
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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引起的。
总结:
这道题花了不少时间。 主要是一开始忘记了递归这一利器。
以上是关于LeetCode 695 Max Area of Island的主要内容,如果未能解决你的问题,请参考以下文章
[Leetcode]695. Max Area of Island
LeetCode 695. Max Area of Island
leetcode-695-Max Area of Island(BFS)
Leetcode刷题记录[java]——695 Max Area of Island