< Summary

Information
Class: LeetCode.Algorithms.WalkingRobotSimulation.WalkingRobotSimulationHashSet
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\WalkingRobotSimulation\WalkingRobotSimulationHashSet.cs
Line coverage
90%
Covered lines: 54
Uncovered lines: 6
Coverable lines: 60
Total lines: 123
Line coverage: 90%
Branch coverage
88%
Covered branches: 24
Total branches: 27
Branch coverage: 88.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
RobotSim(...)100%1212100%
Move(...)80%171577.77%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\WalkingRobotSimulation\WalkingRobotSimulationHashSet.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 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.WalkingRobotSimulation;
 13
 14/// <inheritdoc />
 15public class WalkingRobotSimulationHashSet : IWalkingRobotSimulation
 16{
 17    /// <summary>
 18    ///     Time complexity - O(m + n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="commands"></param>
 22    /// <param name="obstacles"></param>
 23    /// <returns></returns>
 24    public int RobotSim(int[] commands, int[][] obstacles)
 725    {
 726        var result = 0;
 27
 728        var x = 0;
 729        var y = 0;
 30
 731        var direction = 0;
 32
 733        var obstaclesHashSet = new HashSet<(int, int)>();
 34
 6535        foreach (var obstacle in obstacles)
 2236        {
 2237            obstaclesHashSet.Add((obstacle[0], obstacle[1]));
 2238        }
 39
 11540        foreach (var command in commands)
 4741        {
 4742            switch (command)
 43            {
 44                case -1:
 1145                    direction++;
 46
 1147                    if (direction > 3)
 148                    {
 149                        direction = 0;
 150                    }
 51
 1152                    break;
 53
 54                case -2:
 55
 656                    direction--;
 57
 658                    if (direction < 0)
 159                    {
 160                        direction = 3;
 161                    }
 62
 663                    break;
 64                default:
 3065                    var move = Move(direction, x, y, command, obstaclesHashSet);
 66
 3067                    x = move.X;
 3068                    y = move.Y;
 69
 3070                    result = Math.Max(result, (x * x) + (y * y));
 71
 3072                    break;
 73            }
 4774        }
 75
 776        return result;
 777    }
 78
 79    private static (int X, int Y) Move(int direction, int x, int y, int units,
 80        HashSet<(int, int)> obstaclesHashSet)
 3081    {
 48482        for (var i = 0; i < units; i++)
 21383        {
 21384            switch (direction)
 85            {
 86                case 0:
 10387                    if (obstaclesHashSet.Contains((x, y + 1)))
 088                    {
 089                        return (x, y);
 90                    }
 91
 10392                    y++;
 10393                    break;
 94                case 1:
 6895                    if (obstaclesHashSet.Contains((x + 1, y)))
 196                    {
 197                        return (x, y);
 98                    }
 99
 67100                    x++;
 67101                    break;
 102                case 2:
 24103                    if (obstaclesHashSet.Contains((x, y - 1)))
 0104                    {
 0105                        return (x, y);
 106                    }
 107
 24108                    y--;
 24109                    break;
 110                case 3:
 18111                    if (obstaclesHashSet.Contains((x - 1, y)))
 0112                    {
 0113                        return (x, y);
 114                    }
 115
 18116                    x--;
 18117                    break;
 118            }
 212119        }
 120
 29121        return (x, y);
 30122    }
 123}