cs: add «827. Making A Large Island»

URL:	https://leetcode.com/problems/making-a-large-island/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-01-31 13:05:38 +01:00
parent 0cd2d639f1
commit 1a3709064d
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,63 @@
using Coord = (int x, int y);
public class Solution {
private static readonly List<Coord> DIRS = [(0, 1), (0, -1), (1, 0), (-1, 0)];
private static bool InBounds(int[][] grid, Coord pos)
=> pos.y >= 0 && pos.y < grid.Length && pos.x >= 0 && pos.x < grid[pos.y].Length;
private static Coord Add(Coord pos, Coord d) => (pos.x + d.x, pos.y + d.y);
private static IEnumerable<Coord> Coords(int[][] grid) =>
Enumerable
.Range(0, grid.Length)
.SelectMany(
y => Enumerable.Range(0, grid[y].Length).Select(x => (x, y))
);
private static int ExploreIsland(int[][] grid, int island, Coord pos) {
if (!InBounds(grid, pos) || grid[pos.y][pos.x] != 1) {
return 0;
}
grid[pos.y][pos.x] = island;
return 1 + DIRS.Sum(
d => ExploreIsland(grid, island, Add(pos, d))
);
}
public int LargestIsland(int[][] grid) {
var islands = new Dictionary<int, int>();
var islandId = 2;
// Discover islands
foreach (var pos in Coords(grid).Where(pos => grid[pos.y][pos.x] == 1)) {
islands[islandId] = ExploreIsland(grid, islandId, pos);
++islandId;
}
switch (islands.Count) {
case 0:
// can only toggle one cell
return 1;
case 1:
--islandId;
if (islands[islandId] < grid.Length * grid[0].Length) {
// doesn't cover whole grid, can extend by one cell
++islands[islandId];
}
return islands[islandId];
}
return Coords(grid)
.Where(pos => grid[pos.y][pos.x] == 0)
.Max(pos => 1 + DIRS
.Select(d => Add(pos, d))
.Where(pos => InBounds(grid, pos) && grid[pos.y][pos.x] > 1)
.Select(pos => grid[pos.y][pos.x])
.Distinct()
.Sum(id => islands[id])
);
}
}