| | | 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.MinimumTimeToVisitCellInGrid; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class MinimumTimeToVisitCellInGridPriorityQueue : IMinimumTimeToVisitCellInGrid |
| | | 16 | | { |
| | 1 | 17 | | private static readonly IReadOnlyCollection<(int Row, int Column)> Directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Time complexity - O(m * n log m *n) |
| | | 21 | | /// Space complexity - O(m * n) |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="grid"></param> |
| | | 24 | | /// <returns></returns> |
| | | 25 | | public int MinimumTime(int[][] grid) |
| | 3 | 26 | | { |
| | 3 | 27 | | if (grid[0][1] > 1 && grid[1][0] > 1) |
| | 1 | 28 | | { |
| | 1 | 29 | | return -1; |
| | | 30 | | } |
| | | 31 | | |
| | 2 | 32 | | var rowsCount = grid.Length; |
| | 2 | 33 | | var columnsCount = grid[0].Length; |
| | | 34 | | |
| | 2 | 35 | | var priorityQueue = new PriorityQueue<(int Row, int Column, int Time), int>(); |
| | | 36 | | |
| | 2 | 37 | | priorityQueue.Enqueue((0, 0, 0), 0); |
| | | 38 | | |
| | 2 | 39 | | var seen = new bool[rowsCount, columnsCount]; |
| | | 40 | | |
| | 2 | 41 | | seen[0, 0] = true; |
| | | 42 | | |
| | 12 | 43 | | while (priorityQueue.Count > 0) |
| | 12 | 44 | | { |
| | 12 | 45 | | var (row, column, time) = priorityQueue.Dequeue(); |
| | | 46 | | |
| | 126 | 47 | | foreach (var direction in Directions) |
| | 46 | 48 | | { |
| | 46 | 49 | | var targetRow = row + direction.Row; |
| | 46 | 50 | | var targetColumn = column + direction.Column; |
| | | 51 | | |
| | 46 | 52 | | if (targetRow < 0 || |
| | 46 | 53 | | targetRow >= rowsCount || |
| | 46 | 54 | | targetColumn < 0 || |
| | 46 | 55 | | targetColumn >= columnsCount || |
| | 46 | 56 | | seen[targetRow, targetColumn]) |
| | 32 | 57 | | { |
| | 32 | 58 | | continue; |
| | | 59 | | } |
| | | 60 | | |
| | 14 | 61 | | var targetTime = time + 1; |
| | | 62 | | |
| | 14 | 63 | | if (grid[targetRow][targetColumn] > targetTime) |
| | 4 | 64 | | { |
| | 4 | 65 | | var waitTime = 0; |
| | | 66 | | |
| | 4 | 67 | | if ((grid[targetRow][targetColumn] - targetTime) % 2 != 0) |
| | 2 | 68 | | { |
| | 2 | 69 | | waitTime = 1; |
| | 2 | 70 | | } |
| | | 71 | | |
| | 4 | 72 | | targetTime = grid[targetRow][targetColumn] + waitTime; |
| | 4 | 73 | | } |
| | | 74 | | |
| | 14 | 75 | | if (targetRow == rowsCount - 1 && targetColumn == columnsCount - 1) |
| | 2 | 76 | | { |
| | 2 | 77 | | return targetTime; |
| | | 78 | | } |
| | | 79 | | |
| | 12 | 80 | | seen[targetRow, targetColumn] = true; |
| | | 81 | | |
| | 12 | 82 | | priorityQueue.Enqueue((targetRow, targetColumn, targetTime), targetTime); |
| | 12 | 83 | | } |
| | 10 | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | return -1; |
| | 3 | 87 | | } |
| | | 88 | | } |