| | 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.FindMissingAndRepeatedValues; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class FindMissingAndRepeatedValuesHashSet : IFindMissingAndRepeatedValues |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n^2) |
| | 19 | | /// Space complexity - O(n^2) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="grid"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int[] FindMissingAndRepeatedValues(int[][] grid) |
| 2 | 24 | | { |
| 2 | 25 | | var n = grid.Length * grid.Length; |
| | 26 | |
|
| 2 | 27 | | var repeatedNumber = 0; |
| 2 | 28 | | var missingNumber = n * (n + 1) / 2; |
| | 29 | |
|
| 2 | 30 | | var hashSet = new HashSet<int>(); |
| | 31 | |
|
| 16 | 32 | | foreach (var row in grid) |
| 5 | 33 | | { |
| 41 | 34 | | foreach (var cell in row) |
| 13 | 35 | | { |
| 13 | 36 | | if (hashSet.Add(cell)) |
| 11 | 37 | | { |
| 11 | 38 | | missingNumber -= cell; |
| 11 | 39 | | } |
| | 40 | | else |
| 2 | 41 | | { |
| 2 | 42 | | repeatedNumber = cell; |
| 2 | 43 | | } |
| 13 | 44 | | } |
| 5 | 45 | | } |
| | 46 | |
|
| 2 | 47 | | return [repeatedNumber, missingNumber]; |
| 2 | 48 | | } |
| | 49 | | } |