-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMaking A Large Island.cpp
82 lines (65 loc) · 2.63 KB
/
Making A Large Island.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Solution by Rahul Surana
***********************************************************
You are given an n x n binary matrix grid. You are allowed to change at most one 0 to be 1.
Return the size of the largest island in grid after applying this operation.
An island is a 4-directionally connected group of 1s.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
vector<vector<int>> colors,dp;
int dfs(int i, int j, int n,vector<vector<int>>& grid, int color){
if(i >= n || j >= n || i < 0 || j < 0 || colors[i][j] || grid[i][j] == 0) return 0;
colors[i][j] = color;
if(dp[i][j] != -1) return dp[i][j];
int ans = 1;
if(i < n-1 && grid[i+1][j] != 0) ans+= dfs(i+1,j,n,grid,color);
if(j < n-1 && grid[i][j+1] != 0) ans+= dfs(i,j+1,n,grid,color);
if(i > 0 && grid[i-1][j] != 0) ans+= dfs(i-1,j,n,grid,color);
if(j > 0 && grid[i][j-1] != 0) ans+= dfs(i,j-1,n,grid,color);
return dp[i][j] = ans;
}
int largestIsland(vector<vector<int>>& grid) {
int n = grid.size();
if(n == 0) return 0;
colors.resize(n,vector<int>(n,0));
dp.resize(n,vector<int>(n,-1));
int color = 1;
map<int,int> color_area_map;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(grid[i][j] == 1){
color_area_map.insert({color,dfs(i,j,n,grid,color)});
color++;
}
// cout << color_area_map[colors[i][j]] << " "<< colors[i][j] <<" ";
}
// cout << "\n";
}
int ans = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(grid[i][j] == 0){
int m = 1;
set<int> groups;
for(auto x: dir){
int r = i+x[0];
int c = j+x[1];
if((!(r>=n || c >= n || r < 0 || c < 0) && colors[r][c] && groups.count(colors[r][c]) == 0)){
m += color_area_map[colors[r][c]];
groups.insert(colors[r][c]);
}
}
ans = max(ans,m);
// cout << ans <<" ";
// cout << "\n";
}
}
}
if(ans == 0 && grid[0][0] == 1) return n*n;
return ans;
}
};