| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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.MinimumObstacleRemovalToReachCorner; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class MinimumObstacleRemovalToReachCornerBreadthFirstSearch : IMinimumObstacleRemovalToReachCorner |
| | | 16 | | { |
| | 1 | 17 | | private static readonly (int X, int Y)[] Directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Time complexity - O(m * n), where m is the number of rows and n is the number of columns in the grid |
| | | 21 | | /// Space complexity - O(m * n), where m is the number of rows and n is the number of columns in the grid |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="grid"></param> |
| | | 24 | | /// <returns></returns> |
| | | 25 | | public int MinimumObstacles(int[][] grid) |
| | 2 | 26 | | { |
| | 2 | 27 | | var rowsCount = grid.Length; |
| | 2 | 28 | | var columnsCount = grid[0].Length; |
| | | 29 | | |
| | 2 | 30 | | var minimumObstacles = InitializeMinimumObstacles(rowsCount, columnsCount); |
| | | 31 | | |
| | 2 | 32 | | var cellStatesLinkedList = new LinkedList<(int Row, int Column)>(); |
| | | 33 | | |
| | 2 | 34 | | cellStatesLinkedList.AddFirst((0, 0)); |
| | | 35 | | |
| | 26 | 36 | | while (cellStatesLinkedList.First != null) |
| | 24 | 37 | | { |
| | 24 | 38 | | var (row, column) = cellStatesLinkedList.First.Value; |
| | | 39 | | |
| | 24 | 40 | | cellStatesLinkedList.RemoveFirst(); |
| | | 41 | | |
| | 264 | 42 | | foreach (var direction in Directions) |
| | 96 | 43 | | { |
| | 96 | 44 | | var nextRow = row + direction.X; |
| | 96 | 45 | | var nextColumn = column + direction.Y; |
| | | 46 | | |
| | 96 | 47 | | if (nextRow < 0 || nextRow >= rowsCount || nextColumn < 0 || nextColumn >= columnsCount) |
| | 28 | 48 | | { |
| | 28 | 49 | | continue; |
| | | 50 | | } |
| | | 51 | | |
| | 68 | 52 | | var nextObstaclesCount = minimumObstacles[row, column] + grid[nextRow][nextColumn]; |
| | | 53 | | |
| | 68 | 54 | | if (nextObstaclesCount >= minimumObstacles[nextRow, nextColumn]) |
| | 46 | 55 | | { |
| | 46 | 56 | | continue; |
| | | 57 | | } |
| | | 58 | | |
| | 22 | 59 | | minimumObstacles[nextRow, nextColumn] = nextObstaclesCount; |
| | | 60 | | |
| | 22 | 61 | | if (grid[nextRow][nextColumn] == 1) |
| | 10 | 62 | | { |
| | 10 | 63 | | cellStatesLinkedList.AddLast((nextRow, nextColumn)); |
| | 10 | 64 | | } |
| | | 65 | | else |
| | 12 | 66 | | { |
| | 12 | 67 | | cellStatesLinkedList.AddFirst((nextRow, nextColumn)); |
| | 12 | 68 | | } |
| | 22 | 69 | | } |
| | 24 | 70 | | } |
| | | 71 | | |
| | 2 | 72 | | return minimumObstacles[rowsCount - 1, columnsCount - 1]; |
| | 2 | 73 | | } |
| | | 74 | | |
| | | 75 | | private static int[,] InitializeMinimumObstacles(int rowsCount, int columnsCount) |
| | 2 | 76 | | { |
| | 2 | 77 | | var minimumObstacles = new int[rowsCount, columnsCount]; |
| | | 78 | | |
| | 16 | 79 | | for (var row = 0; row < rowsCount; row++) |
| | 6 | 80 | | { |
| | 60 | 81 | | for (var column = 0; column < columnsCount; column++) |
| | 24 | 82 | | { |
| | 24 | 83 | | minimumObstacles[row, column] = int.MaxValue; |
| | 24 | 84 | | } |
| | 6 | 85 | | } |
| | | 86 | | |
| | 2 | 87 | | minimumObstacles[0, 0] = 0; |
| | | 88 | | |
| | 2 | 89 | | return minimumObstacles; |
| | 2 | 90 | | } |
| | | 91 | | } |