Skip to content

Commit 56144df

Browse files
committed
Runtime: 93 ms (Top 5.05%) | Memory: 72.8 MB (Top 5.08%)
1 parent f7136a1 commit 56144df

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

scripts/algorithms/M/Max Area of Island/Max Area of Island.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
1+
// Runtime: 93 ms (Top 5.05%) | Memory: 72.8 MB (Top 5.08%)
12
class Solution {
23
public int maxAreaOfIsland(int[][] grid) {
3-
4+
45
final int rows=grid.length;
56
final int cols=grid[0].length;
67
final int[][] dirrections=new int[][]{{1,0},{0,1},{-1,0},{0,-1}};
7-
Map<String,List<int[]>> adj=new HashMap<>();
8+
Map<String,List<int[]>> adj=new HashMap<>();
89
boolean[][] visited=new boolean[rows][cols];
910
Queue<String> queue=new LinkedList<>();
1011
int res=0;
11-
12+
1213
for(int i=0;i<grid.length;i++)
1314
{
1415
for(int j=0;j<grid[i].length;j++)
1516
{
16-
17+
1718
List<int[]> list=new ArrayList<>();
1819
for(int[] dirrection:dirrections)
1920
{
2021
int newRow=dirrection[0]+i;
2122
int newCol=dirrection[1]+j;
22-
23+
2324
boolean isInBoard=newRow>=rows||newRow<0||newCol>=cols||newCol<0;
2425
if(!isInBoard)
2526
{
2627
list.add(new int[]{newRow,newCol,grid[newRow][newCol]});
2728
}
2829
}
29-
30+
3031
adj.put(getNodeStringFormat(i,j,grid[i][j]),list);
3132
}
32-
}
33-
33+
}
34+
3435
for(int i=0;i<rows;i++)
3536
{
3637
for(int j=0;j<cols;j++)
@@ -43,10 +44,10 @@ public int maxAreaOfIsland(int[][] grid) {
4344
{
4445
String currentStr=queue.poll();
4546
String[] current=currentStr.split(",");
46-
47+
4748
int row=Integer.valueOf(current[0]);
4849
int col=Integer.valueOf(current[1]);
49-
int isLand=Integer.valueOf(current[2]);
50+
int isLand=Integer.valueOf(current[2]);
5051
if(!adj.containsKey(currentStr))
5152
continue;
5253
if(visited[row][col])
@@ -61,14 +62,14 @@ public int maxAreaOfIsland(int[][] grid) {
6162
int newIsLand=item[2];
6263
if(!visited[newRow][newCol] && newIsLand==1 && isLand==1)
6364
queue.add(getNodeStringFormat(newRow,newCol,newIsLand));
64-
}
65+
}
6566
}
6667
res=Math.max(res,count);
6768
}
6869
}
69-
70+
7071
return res;
71-
72+
7273
}
7374
private String getNodeStringFormat(int row,int col,int isLand)
7475
{
@@ -80,4 +81,4 @@ private String getNodeStringFormat(int row,int col,int isLand)
8081
sb.append(isLand);
8182
return sb.toString();
8283
}
83-
}
84+
}

0 commit comments

Comments
 (0)