| | 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 | |
|
| | 12 | | namespace LeetCode.Algorithms.SortTheMatrixDiagonally; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public 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) |
| 2 | 24 | | { |
| 2 | 25 | | var m = mat.Length; |
| 2 | 26 | | var n = mat[0].Length; |
| | 27 | |
|
| 2 | 28 | | var frequencyArray = new int[101]; |
| | 29 | |
|
| 20 | 30 | | for (var row = 0; row < m; row++) |
| 8 | 31 | | { |
| 8 | 32 | | SortDiagonalCount(mat, row, 0, frequencyArray); |
| 8 | 33 | | } |
| | 34 | |
|
| 20 | 35 | | for (var column = 1; column < n; column++) |
| 8 | 36 | | { |
| 8 | 37 | | SortDiagonalCount(mat, 0, column, frequencyArray); |
| 8 | 38 | | } |
| | 39 | |
|
| 2 | 40 | | return mat; |
| 2 | 41 | | } |
| | 42 | |
|
| | 43 | | private static void SortDiagonalCount(int[][] mat, int row, int column, int[] frequencyArray) |
| 16 | 44 | | { |
| 16 | 45 | | var m = mat.Length; |
| 16 | 46 | | var n = mat[0].Length; |
| | 47 | |
|
| 16 | 48 | | var rowIndex = row; |
| 16 | 49 | | var columnIndex = column; |
| | 50 | |
|
| 58 | 51 | | while (rowIndex < m && columnIndex < n) |
| 42 | 52 | | { |
| 42 | 53 | | frequencyArray[mat[rowIndex][columnIndex]]++; |
| | 54 | |
|
| 42 | 55 | | rowIndex++; |
| 42 | 56 | | columnIndex++; |
| 42 | 57 | | } |
| | 58 | |
|
| 16 | 59 | | rowIndex = row; |
| 16 | 60 | | columnIndex = column; |
| | 61 | |
|
| 3264 | 62 | | for (var frequency = 0; frequency < frequencyArray.Length; frequency++) |
| 1616 | 63 | | { |
| 3316 | 64 | | for (var count = frequencyArray[frequency]; count > 0; count--) |
| 42 | 65 | | { |
| 42 | 66 | | mat[rowIndex][columnIndex] = frequency; |
| | 67 | |
|
| 42 | 68 | | rowIndex++; |
| 42 | 69 | | columnIndex++; |
| 42 | 70 | | } |
| 1616 | 71 | | } |
| | 72 | |
|
| 16 | 73 | | Array.Clear(frequencyArray, 0, frequencyArray.Length); |
| 16 | 74 | | } |
| | 75 | | } |