< Summary

Information
Class: LeetCode.Algorithms.SwimInRisingWater.SwimInRisingWaterPriorityQueue
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SwimInRisingWater\SwimInRisingWaterPriorityQueue.cs
Line coverage
100%
Covered lines: 38
Uncovered lines: 0
Coverable lines: 38
Total lines: 81
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
SwimInWater(...)100%2222100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SwimInRisingWater\SwimInRisingWaterPriorityQueue.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.SwimInRisingWater;
 13
 14/// <inheritdoc />
 15public sealed class SwimInRisingWaterPriorityQueue : ISwimInRisingWater
 16{
 117    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)
 226    {
 227        var n = grid.Length;
 28
 229        var minTimes = new int[n, n];
 30
 1831        for (var i = 0; i < n; i++)
 732        {
 7233            for (var j = 0; j < n; j++)
 2934            {
 2935                minTimes[i, j] = int.MaxValue;
 2936            }
 737        }
 38
 239        var startTime = grid[0][0];
 40
 241        minTimes[0, 0] = startTime;
 42
 243        var minTimePriorityQueue = new PriorityQueue<(int X, int Y), int>();
 44
 245        minTimePriorityQueue.Enqueue((0, 0), startTime);
 46
 2147        while (minTimePriorityQueue.Count > 0)
 2148        {
 2149            var (x, y) = minTimePriorityQueue.Dequeue();
 50
 2151            if (x == n - 1 && y == n - 1)
 252            {
 253                break;
 54            }
 55
 20956            foreach (var direction in Directions)
 7657            {
 7658                var nextX = x + direction.X;
 7659                var nextY = y + direction.Y;
 60
 7661                if (nextX < 0 || nextY < 0 || nextX >= n || nextY >= n)
 2262                {
 2263                    continue;
 64                }
 65
 5466                var nextTime = Math.Max(minTimes[x, y], grid[nextX][nextY]);
 67
 5468                if (nextTime >= minTimes[nextX, nextY])
 2769                {
 2770                    continue;
 71                }
 72
 2773                minTimes[nextX, nextY] = nextTime;
 74
 2775                minTimePriorityQueue.Enqueue((nextX, nextY), nextTime);
 2776            }
 1977        }
 78
 279        return minTimes[n - 1, n - 1];
 280    }
 81}