| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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.LuckyNumbersInMatrix; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class LuckyNumbersInMatrixBruteForce : ILuckyNumbersInMatrix |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(m * n) |
| | | 19 | | /// Space complexity - O(m * n) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="matrix"></param> |
| | | 22 | | /// <returns></returns> |
| | | 23 | | public IList<int> LuckyNumbers(int[][] matrix) |
| | 3 | 24 | | { |
| | 11 | 25 | | var minRowItems = matrix.Select(row => row.Min()); |
| | | 26 | | |
| | 3 | 27 | | var transposedMatrix = new int[matrix[0].Length][]; |
| | | 28 | | |
| | 24 | 29 | | for (var i = 0; i < matrix[0].Length; i++) |
| | 9 | 30 | | { |
| | 9 | 31 | | transposedMatrix[i] = new int[matrix.Length]; |
| | 9 | 32 | | } |
| | | 33 | | |
| | 22 | 34 | | for (var i = 0; i < matrix.Length; i++) |
| | 8 | 35 | | { |
| | 66 | 36 | | for (var j = 0; j < matrix[0].Length; j++) |
| | 25 | 37 | | { |
| | 25 | 38 | | transposedMatrix[j][i] = matrix[i][j]; |
| | 25 | 39 | | } |
| | 8 | 40 | | } |
| | | 41 | | |
| | 12 | 42 | | var maxColumnItems = transposedMatrix.Select(column => column.Max()); |
| | 3 | 43 | | var maxColumnItemsHashSet = new HashSet<int>(maxColumnItems); |
| | | 44 | | |
| | 3 | 45 | | return minRowItems.Where(maxColumnItemsHashSet.Contains).ToArray(); |
| | 3 | 46 | | } |
| | | 47 | | } |