| | 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 MinimumNumberOfDaysToDisconnectIslandBruteForceCopyArray : 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(GetArrayCopy(grid)) != 1) |
| 2 | 26 | | { |
| 2 | 27 | | return 0; |
| | 28 | | } |
| | 29 | |
|
| 58 | 30 | | for (var i = 0; i < grid.Length; i++) |
| 25 | 31 | | { |
| 350 | 32 | | for (var j = 0; j < grid[i].Length; j++) |
| 155 | 33 | | { |
| 155 | 34 | | if (grid[i][j] != 1) |
| 44 | 35 | | { |
| 44 | 36 | | continue; |
| | 37 | | } |
| | 38 | |
|
| 111 | 39 | | var newGrid = GetArrayCopy(grid); |
| | 40 | |
|
| 111 | 41 | | newGrid[i][j] = 0; |
| | 42 | |
|
| 111 | 43 | | if (GetIslandsCount(newGrid) != 1) |
| 5 | 44 | | { |
| 5 | 45 | | return 1; |
| | 46 | | } |
| 106 | 47 | | } |
| 20 | 48 | | } |
| | 49 | |
|
| 4 | 50 | | return 2; |
| 11 | 51 | | } |
| | 52 | |
|
| | 53 | | private static int[][] GetArrayCopy(IReadOnlyList<int[]> array) |
| 122 | 54 | | { |
| 122 | 55 | | var newArray = new int[array.Count][]; |
| | 56 | |
|
| 1658 | 57 | | for (var i = 0; i < array.Count; i++) |
| 707 | 58 | | { |
| 707 | 59 | | newArray[i] = new int[array[i].Length]; |
| | 60 | |
|
| 15282 | 61 | | for (var j = 0; j < array[i].Length; j++) |
| 6934 | 62 | | { |
| 6934 | 63 | | newArray[i][j] = array[i][j]; |
| 6934 | 64 | | } |
| 707 | 65 | | } |
| | 66 | |
|
| 122 | 67 | | return newArray; |
| 122 | 68 | | } |
| | 69 | |
|
| | 70 | | private static int GetIslandsCount(IReadOnlyList<int[]> grid) |
| 122 | 71 | | { |
| 122 | 72 | | var islandsCount = 0; |
| | 73 | |
|
| 1658 | 74 | | for (var i = 0; i < grid.Count; i++) |
| 707 | 75 | | { |
| 15282 | 76 | | for (var j = 0; j < grid[i].Length; j++) |
| 6934 | 77 | | { |
| 6934 | 78 | | if (grid[i][j] != 1) |
| 6806 | 79 | | { |
| 6806 | 80 | | continue; |
| | 81 | | } |
| | 82 | |
|
| 128 | 83 | | MarkAsVisited(grid, i, j); |
| | 84 | |
|
| 128 | 85 | | islandsCount++; |
| 128 | 86 | | } |
| 707 | 87 | | } |
| | 88 | |
|
| 122 | 89 | | return islandsCount; |
| 122 | 90 | | } |
| | 91 | |
|
| | 92 | | private static void MarkAsVisited(IReadOnlyList<int[]> grid, int i, int j) |
| 19750 | 93 | | { |
| 19750 | 94 | | if (grid[i][j] != 1) |
| 14070 | 95 | | { |
| 14070 | 96 | | return; |
| | 97 | | } |
| | 98 | |
|
| 5680 | 99 | | grid[i][j] = -1; |
| | 100 | |
|
| 5680 | 101 | | if (i - 1 >= 0) |
| 4841 | 102 | | { |
| 4841 | 103 | | MarkAsVisited(grid, i - 1, j); |
| 4841 | 104 | | } |
| | 105 | |
|
| 5680 | 106 | | if (i + 1 < grid.Count) |
| 4725 | 107 | | { |
| 4725 | 108 | | MarkAsVisited(grid, i + 1, j); |
| 4725 | 109 | | } |
| | 110 | |
|
| 5680 | 111 | | if (j - 1 >= 0) |
| 5028 | 112 | | { |
| 5028 | 113 | | MarkAsVisited(grid, i, j - 1); |
| 5028 | 114 | | } |
| | 115 | |
|
| 5680 | 116 | | if (j + 1 < grid[i].Length) |
| 5028 | 117 | | { |
| 5028 | 118 | | MarkAsVisited(grid, i, j + 1); |
| 5028 | 119 | | } |
| 19750 | 120 | | } |
| | 121 | | } |