| | 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.FirstCompletelyPaintedRowOrColumn; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class FirstCompletelyPaintedRowOrColumnDictionary : IFirstCompletelyPaintedRowOrColumn |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(m * n) |
| | 19 | | /// Space complexity - O(m * n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="arr"></param> |
| | 22 | | /// <param name="mat"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int FirstCompleteIndex(int[] arr, int[][] mat) |
| 2 | 25 | | { |
| 2 | 26 | | var numsDictionary = new Dictionary<int, int>(); |
| | 27 | |
|
| 30 | 28 | | for (var i = 0; i < arr.Length; i++) |
| 13 | 29 | | { |
| 13 | 30 | | numsDictionary.Add(arr[i], i); |
| 13 | 31 | | } |
| | 32 | |
|
| 2 | 33 | | var result = int.MaxValue; |
| 2 | 34 | | var m = mat.Length; |
| 2 | 35 | | var n = mat[0].Length; |
| | 36 | |
|
| 14 | 37 | | for (var i = 0; i < m; i++) |
| 5 | 38 | | { |
| 5 | 39 | | var index = int.MinValue; |
| | 40 | |
|
| 36 | 41 | | for (var j = 0; j < n; j++) |
| 13 | 42 | | { |
| 13 | 43 | | index = Math.Max(index, numsDictionary[mat[i][j]]); |
| 13 | 44 | | } |
| | 45 | |
|
| 5 | 46 | | result = Math.Min(result, index); |
| 5 | 47 | | } |
| | 48 | |
|
| 14 | 49 | | for (var i = 0; i < n; i++) |
| 5 | 50 | | { |
| 5 | 51 | | var index = int.MinValue; |
| | 52 | |
|
| 36 | 53 | | for (var j = 0; j < m; j++) |
| 13 | 54 | | { |
| 13 | 55 | | index = Math.Max(index, numsDictionary[mat[j][i]]); |
| 13 | 56 | | } |
| | 57 | |
|
| 5 | 58 | | result = Math.Min(result, index); |
| 5 | 59 | | } |
| | 60 | |
|
| 2 | 61 | | return result; |
| 2 | 62 | | } |
| | 63 | | } |