GOLANGPROFI Telegram 329
📌 Задача с leetcode. Max Area of Island

Максимальная площадь острова

Сложность: Средняя

Условие задачи:

Дан двумерный массив размера m x n. "1" отвечает за сушу, "0" - за океан. Необходимо опеределить максмимальную площадь острова из островов, расположенных на карте.

Островом считается территория, образованная из "1", расположенных сверху, справа, снизу и слева относительно друг друга.

Пример:

Ввод:
grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]

Вывод: 6

Ввод: grid = [[0,0,0,0,0,0,0,0]]

Вывод: 0

Решение

func maxAreaOfIsland(grid [][]int) int {
rows := len(grid)
if rows == 0 {
return 0
}

cols := len(grid[0])

var dfs func(grid [][]int, x, y, r, c, area int) int
dfs = func(grid [][]int, x, y, r, c, area int) int {
if x < 0 || y < 0 || x >= r || y >= c || grid[x][y] != 1 {
return area
}
grid[x][y] = 2

return 1 + dfs(grid, x+1, y, r, c, area) + dfs(grid, x, y+1, r, c, area) + dfs(grid, x-1, y, r, c, area) + dfs(grid, x, y-1, r, c, area)
}

maxArea := 0
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
area := dfs(grid, i, j, rows, cols, 0)
maxArea = int(math.Max(float64(area), float64(maxArea)))
}
}
return maxArea
}

Временная сложность: O(N*M)
Пространственная сложность: O(1)

Пишите свое решение в комментариях👇



tgoop.com/golangprofi/329
Create:
Last Update:

📌 Задача с leetcode. Max Area of Island

Максимальная площадь острова

Сложность: Средняя

Условие задачи:

Дан двумерный массив размера m x n. "1" отвечает за сушу, "0" - за океан. Необходимо опеределить максмимальную площадь острова из островов, расположенных на карте.

Островом считается территория, образованная из "1", расположенных сверху, справа, снизу и слева относительно друг друга.

Пример:

Ввод:
grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]

Вывод: 6

Ввод: grid = [[0,0,0,0,0,0,0,0]]

Вывод: 0

Решение

func maxAreaOfIsland(grid [][]int) int {
rows := len(grid)
if rows == 0 {
return 0
}

cols := len(grid[0])

var dfs func(grid [][]int, x, y, r, c, area int) int
dfs = func(grid [][]int, x, y, r, c, area int) int {
if x < 0 || y < 0 || x >= r || y >= c || grid[x][y] != 1 {
return area
}
grid[x][y] = 2

return 1 + dfs(grid, x+1, y, r, c, area) + dfs(grid, x, y+1, r, c, area) + dfs(grid, x-1, y, r, c, area) + dfs(grid, x, y-1, r, c, area)
}

maxArea := 0
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
area := dfs(grid, i, j, rows, cols, 0)
maxArea = int(math.Max(float64(area), float64(maxArea)))
}
}
return maxArea
}

Временная сложность: O(N*M)
Пространственная сложность: O(1)

Пишите свое решение в комментариях👇

BY Golang Юниор




Share with your friend now:
tgoop.com/golangprofi/329

View MORE
Open in Telegram


Telegram News

Date: |

Earlier, crypto enthusiasts had created a self-described “meme app” dubbed “gm” app wherein users would greet each other with “gm” or “good morning” messages. However, in September 2021, the gm app was down after a hacker reportedly gained access to the user data. Matt Hussey, editorial director at NEAR Protocol also responded to this news with “#meIRL”. Just as you search “Bear Market Screaming” in Telegram, you will see a Pepe frog yelling as the group’s featured image. Telegram is a leading cloud-based instant messages platform. It became popular in recent years for its privacy, speed, voice and video quality, and other unmatched features over its main competitor Whatsapp. Commenting about the court's concerns about the spread of false information related to the elections, Minister Fachin noted Brazil is "facing circumstances that could put Brazil's democracy at risk." During the meeting, the information technology secretary at the TSE, Julio Valente, put forward a list of requests the court believes will disinformation. Telegram iOS app: In the “Chats” tab, click the new message icon in the right upper corner. Select “New Channel.”
from us


Telegram Golang Юниор
FROM American