< Summary

Information
Class: LeetCode.Algorithms.DiagonalTraverse.DiagonalTraverseSimulation
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DiagonalTraverse\DiagonalTraverseSimulation.cs
Line coverage
100%
Covered lines: 36
Uncovered lines: 0
Coverable lines: 36
Total lines: 75
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FindDiagonalOrder(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DiagonalTraverse\DiagonalTraverseSimulation.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.DiagonalTraverse;
 13
 14/// <inheritdoc />
 15public class DiagonalTraverseSimulation : IDiagonalTraverse
 16{
 17    /// <summary>
 18    ///     Time complexity - O(m * n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="mat"></param>
 22    /// <returns></returns>
 23    public int[] FindDiagonalOrder(int[][] mat)
 224    {
 225        var m = mat.Length;
 226        var n = mat[0].Length;
 27
 228        var result = new int[m * n];
 29
 230        var row = 0;
 231        var column = 0;
 232        var direction = Direction.UpRight;
 33
 3034        for (var i = 0; i < result.Length; i++)
 1335        {
 1336            result[i] = mat[row][column];
 37
 1338            switch (direction)
 39            {
 740                case Direction.UpRight when column == n - 1:
 341                    row++;
 342                    direction = Direction.DownLeft;
 343                    break;
 444                case Direction.UpRight when row == 0:
 245                    column++;
 246                    direction = Direction.DownLeft;
 247                    break;
 48                case Direction.UpRight:
 249                    row--;
 250                    column++;
 251                    break;
 652                case Direction.DownLeft when row == m - 1:
 253                    column++;
 254                    direction = Direction.UpRight;
 255                    break;
 456                case Direction.DownLeft when column == 0:
 157                    row++;
 158                    direction = Direction.UpRight;
 159                    break;
 60                case Direction.DownLeft:
 361                    row++;
 362                    column--;
 363                    break;
 64            }
 1365        }
 66
 267        return result;
 268    }
 69
 70    private enum Direction
 71    {
 72        UpRight,
 73        DownLeft
 74    }
 75}