-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path31_Making_A_Large_Island.cpp
181 lines (152 loc) · 6.08 KB
/
31_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// 827. Making A Large Island
// Solved
// Hard
// Topics
// Companies
// 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.
// Example 1:
// Input: grid = [[1,0],[0,1]]
// Output: 3
// Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
// Example 2:
// Input: grid = [[1,1],[1,0]]
// Output: 4
// Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 4.
// Example 3:
// Input: grid = [[1,1],[1,1]]
// Output: 4
// Explanation: Can't change any 0 to 1, only one island with area = 4.
// Constraints:
// n == grid.length
// n == grid[i].length
// 1 <= n <= 500
// grid[i][j] is either 0 or 1.
class Solution
{
private:
int exploreIsland(vector<vector<int>> &grid, int islandId, int currentRow,
int currentColumn)
{
if (currentRow < 0 || currentRow >= grid.size() || currentColumn < 0 ||
currentColumn >= grid[0].size() ||
grid[currentRow][currentColumn] != 1)
return 0;
grid[currentRow][currentColumn] = islandId;
return 1 +
exploreIsland(grid, islandId, currentRow + 1, currentColumn) +
exploreIsland(grid, islandId, currentRow - 1, currentColumn) +
exploreIsland(grid, islandId, currentRow, currentColumn + 1) +
exploreIsland(grid, islandId, currentRow, currentColumn - 1);
}
public:
int largestIsland(vector<vector<int>> &grid)
{
unordered_map<int, int> islandSizes;
int islandId = 2;
// Step 1: Mark all islands and calculate their sizes
for (int currentRow = 0; currentRow < grid.size(); ++currentRow)
{
for (int currentColumn = 0; currentColumn < grid[0].size();
++currentColumn)
{
if (grid[currentRow][currentColumn] == 1)
{
islandSizes[islandId] = exploreIsland(
grid, islandId, currentRow, currentColumn);
++islandId;
}
}
}
// If there are no islands, return 1
if (islandSizes.empty())
{
return 1;
}
// If the entire grid is one island, return its size or size + 1
if (islandSizes.size() == 1)
{
--islandId;
return (islandSizes[islandId] == grid.size() * grid[0].size())
? islandSizes[islandId]
: islandSizes[islandId] + 1;
}
int maxIslandSize = 1;
// Step 2: Try converting every 0 to 1 and calculate the resulting
// island size
for (int currentRow = 0; currentRow < grid.size(); ++currentRow)
{
for (int currentColumn = 0; currentColumn < grid[0].size();
++currentColumn)
{
if (grid[currentRow][currentColumn] == 0)
{
int currentIslandSize = 1;
unordered_set<int> neighboringIslands;
// Check down
if (currentRow + 1 < grid.size() &&
grid[currentRow + 1][currentColumn] > 1)
{
neighboringIslands.insert(
grid[currentRow + 1][currentColumn]);
}
// Check up
if (currentRow - 1 >= 0 &&
grid[currentRow - 1][currentColumn] > 1)
{
neighboringIslands.insert(
grid[currentRow - 1][currentColumn]);
}
// Check right
if (currentColumn + 1 < grid[0].size() &&
grid[currentRow][currentColumn + 1] > 1)
{
neighboringIslands.insert(
grid[currentRow][currentColumn + 1]);
}
// Check left
if (currentColumn - 1 >= 0 &&
grid[currentRow][currentColumn - 1] > 1)
{
neighboringIslands.insert(
grid[currentRow][currentColumn - 1]);
}
// Sum the sizes of all unique neighboring islands
for (int id : neighboringIslands)
{
currentIslandSize += islandSizes[id];
}
maxIslandSize = max(maxIslandSize, currentIslandSize);
}
}
}
return maxIslandSize;
}
};
/*
This code solves the problem of finding the largest possible island in a binary matrix after changing at most one 0 to 1. Here's how it works:
1. The exploreIsland function uses DFS to explore and mark connected 1s as part of the same island. It:
- Takes parameters: grid, islandId (unique identifier for each island), and current position
- Returns the size of the explored island
- Marks visited cells with the islandId instead of 1
2. The largestIsland function:
a) First pass (Step 1):
- Identifies all existing islands
- Assigns unique IDs (starting from 2) to each island
- Stores island sizes in islandSizes map
b) Special cases:
- If no islands exist, returns 1
- If one island covers entire grid, returns its size
- If one island with empty cells exists, returns size + 1
c) Second pass (Step 2):
- Examines each 0 cell
- Checks all 4 directions for neighboring islands
- Calculates potential island size by combining neighboring islands
- Keeps track of maximum possible island size
3. Key data structures:
- islandSizes: Maps island IDs to their sizes
- neighboringIslands: Tracks unique neighboring islands for each 0 cell
4. Time Complexity: O(N²) where N is the grid dimension
Space Complexity: O(N²) for storing island information
*/