| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.MinimumNumberOfDaysToDisconnectIsland; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MinimumNumberOfDaysToDisconnectIslandBruteForceVisitedArray : IMinimumNumberOfDaysToDisconnectIsland |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n^2 * m^2) |
| | 19 | | /// Space complexity - O(n * m) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="grid"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int MinDays(int[][] grid) |
| 11 | 24 | | { |
| 11 | 25 | | if (GetIslandsCount(grid) != 1) |
| 2 | 26 | | { |
| 2 | 27 | | return 0; |
| | 28 | | } |
| | 29 | |
|
| 72 | 30 | | foreach (var row in grid) |
| 25 | 31 | | { |
| 350 | 32 | | for (var j = 0; j < row.Length; j++) |
| 155 | 33 | | { |
| 155 | 34 | | if (row[j] != 1) |
| 44 | 35 | | { |
| 44 | 36 | | continue; |
| | 37 | | } |
| | 38 | |
|
| 111 | 39 | | row[j] = 0; |
| | 40 | |
|
| 111 | 41 | | if (GetIslandsCount(grid) != 1) |
| 5 | 42 | | { |
| 5 | 43 | | return 1; |
| | 44 | | } |
| | 45 | |
|
| 106 | 46 | | row[j] = 1; |
| 106 | 47 | | } |
| 20 | 48 | | } |
| | 49 | |
|
| 4 | 50 | | return 2; |
| 11 | 51 | | } |
| | 52 | |
|
| | 53 | | private static int GetIslandsCount(IReadOnlyList<int[]> grid) |
| 122 | 54 | | { |
| 122 | 55 | | var islandsCount = 0; |
| | 56 | |
|
| 122 | 57 | | var visited = new bool[grid.Count][]; |
| | 58 | |
|
| 1658 | 59 | | for (var i = 0; i < grid.Count; i++) |
| 707 | 60 | | { |
| 707 | 61 | | visited[i] = new bool[grid[i].Length]; |
| 707 | 62 | | } |
| | 63 | |
|
| 1658 | 64 | | for (var i = 0; i < grid.Count; i++) |
| 707 | 65 | | { |
| 15282 | 66 | | for (var j = 0; j < grid[i].Length; j++) |
| 6934 | 67 | | { |
| 6934 | 68 | | if (grid[i][j] != 1 || visited[i][j]) |
| 6806 | 69 | | { |
| 6806 | 70 | | continue; |
| | 71 | | } |
| | 72 | |
|
| 128 | 73 | | MarkAsVisited(grid, visited, i, j); |
| | 74 | |
|
| 128 | 75 | | islandsCount++; |
| 128 | 76 | | } |
| 707 | 77 | | } |
| | 78 | |
|
| 122 | 79 | | return islandsCount; |
| 122 | 80 | | } |
| | 81 | |
|
| | 82 | | private static void MarkAsVisited(IReadOnlyList<int[]> grid, IReadOnlyList<bool[]> visited, int i, int j) |
| 19750 | 83 | | { |
| 19750 | 84 | | if (grid[i][j] == 0 || visited[i][j]) |
| 14070 | 85 | | { |
| 14070 | 86 | | return; |
| | 87 | | } |
| | 88 | |
|
| 5680 | 89 | | visited[i][j] = true; |
| | 90 | |
|
| 5680 | 91 | | if (i - 1 >= 0) |
| 4841 | 92 | | { |
| 4841 | 93 | | MarkAsVisited(grid, visited, i - 1, j); |
| 4841 | 94 | | } |
| | 95 | |
|
| 5680 | 96 | | if (i + 1 < grid.Count) |
| 4725 | 97 | | { |
| 4725 | 98 | | MarkAsVisited(grid, visited, i + 1, j); |
| 4725 | 99 | | } |
| | 100 | |
|
| 5680 | 101 | | if (j - 1 >= 0) |
| 5028 | 102 | | { |
| 5028 | 103 | | MarkAsVisited(grid, visited, i, j - 1); |
| 5028 | 104 | | } |
| | 105 | |
|
| 5680 | 106 | | if (j + 1 < grid[i].Length) |
| 5028 | 107 | | { |
| 5028 | 108 | | MarkAsVisited(grid, visited, i, j + 1); |
| 5028 | 109 | | } |
| 19750 | 110 | | } |
| | 111 | | } |