827. Making A Large Island #1250
-
Topics: You are given an Return the size of the largest island in An island is a 4-directionally connected group of Example 1:
Example 2:
Example 3:
Constraints:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
To solve the problem of making a large island by changing at most one
Let's implement this solution in PHP: 827. Making A Large Island <?php
/**
* @param Integer[][] $grid
* @return Integer
*/
function largestIsland($grid) {
$n = count($grid);
$islandId = 2; // Start assigning island IDs from 2
$islandSizes = array(); // To store the size of each island
// Step 1: Identify islands and calculate their sizes
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $n; $j++) {
if ($grid[$i][$j] == 1) {
$size = 0;
$this->dfs($grid, $i, $j, $islandId, $size);
$islandSizes[$islandId] = $size;
$islandId++;
}
}
}
// Step 2: Find the best 0 to flip
$maxSize = 0;
foreach ($islandSizes as $size) {
$maxSize = max($maxSize, $size);
}
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $n; $j++) {
if ($grid[$i][$j] == 0) {
$neighbors = array();
$potentialSize = 1;
// Check all four directions
if ($i > 0 && $grid[$i-1][$j] > 1) {
$neighbors[$grid[$i-1][$j]] = true;
}
if ($i < $n-1 && $grid[$i+1][$j] > 1) {
$neighbors[$grid[$i+1][$j]] = true;
}
if ($j > 0 && $grid[$i][$j-1] > 1) {
$neighbors[$grid[$i][$j-1]] = true;
}
if ($j < $n-1 && $grid[$i][$j+1] > 1) {
$neighbors[$grid[$i][$j+1]] = true;
}
foreach ($neighbors as $id => $val) {
$potentialSize += $islandSizes[$id];
}
$maxSize = max($maxSize, $potentialSize);
}
}
}
return $maxSize;
}
/**
* Helper function to perform DFS and calculate component size
*
* @param $grid
* @param $i
* @param $j
* @param $islandId
* @param $size
* @return void
*/
function dfs(&$grid, $i, $j, $islandId, &$size) {
$n = count($grid);
if ($i < 0 || $i >= $n || $j < 0 || $j >= $n || $grid[$i][$j] != 1) {
return;
}
$grid[$i][$j] = $islandId;
$size++;
// Explore all four directions
$this->dfs($grid, $i-1, $j, $islandId, $size);
$this->dfs($grid, $i+1, $j, $islandId, $size);
$this->dfs($grid, $i, $j-1, $islandId, $size);
$this->dfs($grid, $i, $j+1, $islandId, $size);
}
// Example usage
$grid1 = [[1, 0], [0, 1]];
$grid2 = [[1, 1], [1, 0]];
$grid3 = [[1, 1], [1, 1]];
echo largestIsland($grid1) . "\n"; // Output: 3
echo largestIsland($grid2) . "\n"; // Output: 4
echo largestIsland($grid3) . "\n"; // Output: 4
?> Explanation:
This solution efficiently handles the problem constraints and provides the correct result. |
Beta Was this translation helpful? Give feedback.
To solve the problem of making a large island by changing at most one
0
to1
in a binary matrix, we can follow these steps:Identify Islands: First, we need to identify all the existing islands in the grid and assign a unique identifier to each island. We can use Depth-First Search (DFS) or Breadth-First Search (BFS) to traverse the grid and mark each cell of an island with a unique ID.
Calculate Island Sizes: As we identify each island, we calculate its size and store it in a dictionary or array where the key is the island ID and the value is the size of the island.
Find the Best 0 to Flip: For each
0
in the grid, we check its neighboring cells to see if they belong to different is…