< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
DiagonalSort(...)100%44100%
SortDiagonalCount(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SortTheMatrixDiagonally\SortTheMatrixDiagonallyFrequencyArray.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.SortTheMatrixDiagonally;
 13
 14/// <inheritdoc />
 15public class SortTheMatrixDiagonallyFrequencyArray : ISortTheMatrixDiagonally
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n * m)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="mat"></param>
 22    /// <returns></returns>
 23    public int[][] DiagonalSort(int[][] mat)
 224    {
 225        var m = mat.Length;
 226        var n = mat[0].Length;
 27
 228        var frequencyArray = new int[101];
 29
 2030        for (var row = 0; row < m; row++)
 831        {
 832            SortDiagonalCount(mat, row, 0, frequencyArray);
 833        }
 34
 2035        for (var column = 1; column < n; column++)
 836        {
 837            SortDiagonalCount(mat, 0, column, frequencyArray);
 838        }
 39
 240        return mat;
 241    }
 42
 43    private static void SortDiagonalCount(int[][] mat, int row, int column, int[] frequencyArray)
 1644    {
 1645        var m = mat.Length;
 1646        var n = mat[0].Length;
 47
 1648        var rowIndex = row;
 1649        var columnIndex = column;
 50
 5851        while (rowIndex < m && columnIndex < n)
 4252        {
 4253            frequencyArray[mat[rowIndex][columnIndex]]++;
 54
 4255            rowIndex++;
 4256            columnIndex++;
 4257        }
 58
 1659        rowIndex = row;
 1660        columnIndex = column;
 61
 326462        for (var frequency = 0; frequency < frequencyArray.Length; frequency++)
 161663        {
 331664            for (var count = frequencyArray[frequency]; count > 0; count--)
 4265            {
 4266                mat[rowIndex][columnIndex] = frequency;
 67
 4268                rowIndex++;
 4269                columnIndex++;
 4270            }
 161671        }
 72
 1673        Array.Clear(frequencyArray, 0, frequencyArray.Length);
 1674    }
 75}