| | | 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 MinimumObstacleRemovalToReachCornerPriorityQueue : 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 * log(m * n)), where m is the number of rows and n is the number of columns in the |
| | | 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 cellStatesPriorityQueue = new PriorityQueue<(int Row, int Column), int>(); |
| | | 33 | | |
| | 2 | 34 | | cellStatesPriorityQueue.Enqueue((0, 0), 0); |
| | | 35 | | |
| | 19 | 36 | | while (cellStatesPriorityQueue.Count > 0) |
| | 19 | 37 | | { |
| | 19 | 38 | | var (row, column) = cellStatesPriorityQueue.Dequeue(); |
| | | 39 | | |
| | 19 | 40 | | if (row == rowsCount - 1 && column == columnsCount - 1) |
| | 2 | 41 | | { |
| | 2 | 42 | | break; |
| | | 43 | | } |
| | | 44 | | |
| | 187 | 45 | | foreach (var direction in Directions) |
| | 68 | 46 | | { |
| | 68 | 47 | | var nextRow = row + direction.X; |
| | 68 | 48 | | var nextColumn = column + direction.Y; |
| | | 49 | | |
| | 68 | 50 | | if (nextRow < 0 || nextRow >= rowsCount || nextColumn < 0 || nextColumn >= columnsCount) |
| | 21 | 51 | | { |
| | 21 | 52 | | continue; |
| | | 53 | | } |
| | | 54 | | |
| | 47 | 55 | | var nextObstaclesCount = minimumObstacles[row, column] + grid[nextRow][nextColumn]; |
| | | 56 | | |
| | 47 | 57 | | if (nextObstaclesCount >= minimumObstacles[nextRow, nextColumn]) |
| | 25 | 58 | | { |
| | 25 | 59 | | continue; |
| | | 60 | | } |
| | | 61 | | |
| | 22 | 62 | | minimumObstacles[nextRow, nextColumn] = nextObstaclesCount; |
| | | 63 | | |
| | 22 | 64 | | cellStatesPriorityQueue.Enqueue((nextRow, nextColumn), nextObstaclesCount); |
| | 22 | 65 | | } |
| | 17 | 66 | | } |
| | | 67 | | |
| | 2 | 68 | | return minimumObstacles[rowsCount - 1, columnsCount - 1]; |
| | 2 | 69 | | } |
| | | 70 | | |
| | | 71 | | private static int[,] InitializeMinimumObstacles(int rowsCount, int columnsCount) |
| | 2 | 72 | | { |
| | 2 | 73 | | var minimumObstacles = new int[rowsCount, columnsCount]; |
| | | 74 | | |
| | 16 | 75 | | for (var row = 0; row < rowsCount; row++) |
| | 6 | 76 | | { |
| | 60 | 77 | | for (var column = 0; column < columnsCount; column++) |
| | 24 | 78 | | { |
| | 24 | 79 | | minimumObstacles[row, column] = int.MaxValue; |
| | 24 | 80 | | } |
| | 6 | 81 | | } |
| | | 82 | | |
| | 2 | 83 | | minimumObstacles[0, 0] = 0; |
| | | 84 | | |
| | 2 | 85 | | return minimumObstacles; |
| | 2 | 86 | | } |
| | | 87 | | } |