< Summary

Information
Class: LeetCode.Algorithms.SortMatrixByDiagonals.SortMatrixByDiagonalsSimulation
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SortMatrixByDiagonals\SortMatrixByDiagonalsSimulation.cs
Line coverage
100%
Covered lines: 36
Uncovered lines: 0
Coverable lines: 36
Total lines: 74
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
SortMatrix(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SortMatrixByDiagonals\SortMatrixByDiagonalsSimulation.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.SortMatrixByDiagonals;
 13
 14/// <inheritdoc />
 15public class SortMatrixByDiagonalsSimulation : ISortMatrixByDiagonals
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n^2 log n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="grid"></param>
 22    /// <returns></returns>
 23    public int[][] SortMatrix(int[][] grid)
 424    {
 425        var n = grid.Length;
 26
 427        if (n == 1)
 128        {
 129            return grid;
 30        }
 31
 332        Span<int> items = stackalloc int[n * n];
 33
 1634        for (var i = 0; i < n - 1; i++)
 535        {
 536            var index = 0;
 37
 3438            for (var j = 0; i + j < n; j++)
 1239            {
 1240                items[index] = grid[i + j][j];
 41
 1242                index++;
 1243            }
 44
 1445            items[..index].Sort((a, b) => b.CompareTo(a));
 46
 3447            for (var j = 0; j < index; j++)
 1248            {
 1249                grid[i + j][j] = items[j];
 1250            }
 551        }
 52
 1053        for (var j = 1; j < n - 1; j++)
 254        {
 255            var index = 0;
 56
 1257            for (var i = 0; j + i < n; i++)
 458            {
 459                items[index] = grid[i][j + i];
 60
 461                index++;
 462            }
 63
 264            items[..index].Sort();
 65
 1266            for (var i = 0; i < index; i++)
 467            {
 468                grid[i][j + i] = items[i];
 469            }
 270        }
 71
 372        return grid;
 473    }
 74}

Methods/Properties

SortMatrix(System.Int32[][])