2023-05-07:给你一个大小为 n x n 二进制矩阵 grid 。最多 只能将一格 0 变成 1 。
返回执行此操作后,grid 中最大的岛屿面积是多少?
岛屿 由一组上、下、左、右四个方向相连的 1 形成。
输入: grid = [[1, 0], [0, 1]]。
输出: 3。
来自亚马逊、谷歌、微软、Facebook、Bloomberg。
答案2023-05-07:
算法步骤:
1.遍历输入矩阵 grid
,对于每个岛屿进行标记,并用数组 sizes
统计每个岛屿的大小。
2.遍历矩阵 grid
,对于每个位置上的值,如果当前位置上的值为非零正整数,则更新答案为当前岛屿的大小。
3.遍历矩阵 grid
,当当前位置上的值为 0 时,分别查看该位置上、下、左、右四个方向是否有与其相邻且已经被访问过的岛屿,并将它们的大小累加起来。如果这些岛屿的大小之和加上当前位置上自身的大小可以更新最大岛屿面积,则更新答案。
4.返回答案。
时间复杂度: ,遍历了三次矩阵,每次遍历的时间复杂度均为 。
空间复杂度:,使用了两个二维数组,每个数组都是 的大小。
go完整代码如下:
package main
import "fmt"
func main() {
grid := [][]int{{1, 0}, {0, 1}}
ans := largestIsland(grid)
fmt.Println(ans)
}
func largestIsland(grid [][]int) int {
n := len(grid)
m := len(grid[0])
id := 2
for i := 0; i 1 {
sizes[grid[i][j]]++
ans = max(ans, sizes[grid[i][j]])
}
}
}
visited := make([]bool, id)
for i := 0; i = 0 {
up = grid[i-1][j]
}
down := 0
if i+1 = 0 {
left = grid[i][j-1]
}
right := 0
if j+1 b {
return a
}
return b
}
rust完整代码如下:
fn main() {
let grid = vec![vec![1, 0], vec![0, 1]];
let ans = largest_island(grid);
println!("{}", ans);
}
fn largest_island(grid: Vec>) -> i32 {
let n = grid.len();
let m = grid[0].len();
let mut id = 2;
let mut new_grid = grid.clone();
for i in 0..n {
for j in 0..m {
if new_grid[i][j] == 1 {
infect(&mut new_grid, i as i32, j as i32, id, n as i32, m as i32);
id += 1;
}
}
}
let mut sizes = vec![0; id as usize];
let mut ans = 0;
for i in 0..n {
for j in 0..m {
if new_grid[i][j] > 1 {
sizes[new_grid[i][j] as usize] += 1;
ans = ans.max(sizes[new_grid[i][j] as usize]);
}
}
}
let mut visited = vec![false; id as usize];
for i in 0..n {
for j in 0..m {
if new_grid[i][j] == 0 {
let up = if i > 0 { new_grid[i - 1][j] } else { 0 };
let down = if i 0 { new_grid[i][j - 1] } else { 0 };
let right = if j 0 && !visited[up as usize] {
visited[up as usize] = true;
merge += sizes[up as usize];
}
if down > 0 && !visited[down as usize] {
visited[down as usize] = true;
merge += sizes[down as usize];
}
if left > 0 && !visited[left as usize] {
visited[left as usize] = true;
merge += sizes[left as usize];
}
if right > 0 && !visited[right as usize] {
visited[right as usize] = true;
merge += sizes[right as usize];
}
ans = ans.max(merge);
visited[up as usize] = false;
visited[down as usize] = false;
visited[left as usize] = false;
visited[right as usize] = false;
}
}
}
ans
}
fn infect(grid: &mut Vec>, i: i32, j: i32, v: i32, n: i32, m: i32) {
if i
c完整代码如下:
#include
#include
#include
#define MAX_SIZE 50
void infect(int grid[][MAX_SIZE], int i, int j, int v, int n, int m);
int largestIsland(int grid[][MAX_SIZE], int n, int m);
int main() {
int grid[][MAX_SIZE] = { {1, 0}, {0, 1} };
int n = 2;
int m = 2;
int ans = largestIsland(grid, n, m);
printf("%dn", ans); // 输出 3
return 0;
}
int largestIsland(int grid[][MAX_SIZE], int n, int m) {
int id = 2;
for (int i = 0; i 1) {
ans = ans > ++sizes[grid[i][j]] ? ans : sizes[grid[i][j]];
}
}
}
bool visited[MAX_SIZE * MAX_SIZE] = { false };
for (int i = 0; i = 0 ? grid[i - 1][j] : 0;
int down = i + 1 = 0 ? grid[i][j - 1] : 0;
int right = j + 1 merge ? ans : merge;
visited[up] = false;
visited[down] = false;
visited[left] = false;
visited[right] = false;
}
}
}
return ans;
}
void infect(int grid[][MAX_SIZE], int i, int j, int v, int n, int m) {
if (i
c++完整代码如下:
#include
#include
#define MAX_SIZE 50
using namespace std;
void infect(int grid[][MAX_SIZE], int i, int j, int v, int n, int m);
int largestIsland(int grid[][MAX_SIZE], int n, int m);
int main() {
int grid[][MAX_SIZE] = { {1, 0}, {0, 1} };
int n = 2;
int m = 2;
int ans = largestIsland(grid, n, m);
cout 1) {
ans = max(ans, ++sizes[grid[i][j]]);
}
}
}
bool visited[MAX_SIZE * MAX_SIZE] = { false }; // 初始化为false
for (int i = 0; i = 0 ? grid[i - 1][j] : 0;
int down = i + 1 = 0 ? grid[i][j - 1] : 0;
int right = j + 1
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net