| | | 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.SwimInRisingWater; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class SwimInRisingWaterPriorityQueue : ISwimInRisingWater |
| | | 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(n^2 log(n)) |
| | | 21 | | /// Space complexity - O(n^2) |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="grid"></param> |
| | | 24 | | /// <returns></returns> |
| | | 25 | | public int SwimInWater(int[][] grid) |
| | 2 | 26 | | { |
| | 2 | 27 | | var n = grid.Length; |
| | | 28 | | |
| | 2 | 29 | | var minTimes = new int[n, n]; |
| | | 30 | | |
| | 18 | 31 | | for (var i = 0; i < n; i++) |
| | 7 | 32 | | { |
| | 72 | 33 | | for (var j = 0; j < n; j++) |
| | 29 | 34 | | { |
| | 29 | 35 | | minTimes[i, j] = int.MaxValue; |
| | 29 | 36 | | } |
| | 7 | 37 | | } |
| | | 38 | | |
| | 2 | 39 | | var startTime = grid[0][0]; |
| | | 40 | | |
| | 2 | 41 | | minTimes[0, 0] = startTime; |
| | | 42 | | |
| | 2 | 43 | | var minTimePriorityQueue = new PriorityQueue<(int X, int Y), int>(); |
| | | 44 | | |
| | 2 | 45 | | minTimePriorityQueue.Enqueue((0, 0), startTime); |
| | | 46 | | |
| | 21 | 47 | | while (minTimePriorityQueue.Count > 0) |
| | 21 | 48 | | { |
| | 21 | 49 | | var (x, y) = minTimePriorityQueue.Dequeue(); |
| | | 50 | | |
| | 21 | 51 | | if (x == n - 1 && y == n - 1) |
| | 2 | 52 | | { |
| | 2 | 53 | | break; |
| | | 54 | | } |
| | | 55 | | |
| | 209 | 56 | | foreach (var direction in Directions) |
| | 76 | 57 | | { |
| | 76 | 58 | | var nextX = x + direction.X; |
| | 76 | 59 | | var nextY = y + direction.Y; |
| | | 60 | | |
| | 76 | 61 | | if (nextX < 0 || nextY < 0 || nextX >= n || nextY >= n) |
| | 22 | 62 | | { |
| | 22 | 63 | | continue; |
| | | 64 | | } |
| | | 65 | | |
| | 54 | 66 | | var nextTime = Math.Max(minTimes[x, y], grid[nextX][nextY]); |
| | | 67 | | |
| | 54 | 68 | | if (nextTime >= minTimes[nextX, nextY]) |
| | 27 | 69 | | { |
| | 27 | 70 | | continue; |
| | | 71 | | } |
| | | 72 | | |
| | 27 | 73 | | minTimes[nextX, nextY] = nextTime; |
| | | 74 | | |
| | 27 | 75 | | minTimePriorityQueue.Enqueue((nextX, nextY), nextTime); |
| | 27 | 76 | | } |
| | 19 | 77 | | } |
| | | 78 | | |
| | 2 | 79 | | return minTimes[n - 1, n - 1]; |
| | 2 | 80 | | } |
| | | 81 | | } |