| | 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.SetMatrixZeroes; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class SetMatrixZeroesArrayMarkers : ISetMatrixZeroes |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(m * n) |
| | 19 | | /// Space complexity - O(m + n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="matrix"></param> |
| | 22 | | public void SetZeroes(int[][] matrix) |
| 2 | 23 | | { |
| 2 | 24 | | var rows = new bool[matrix.Length]; |
| 2 | 25 | | var columns = new bool[matrix[0].Length]; |
| | 26 | |
|
| 16 | 27 | | for (var i = 0; i < matrix.Length; i++) |
| 6 | 28 | | { |
| 54 | 29 | | for (var j = 0; j < matrix[i].Length; j++) |
| 21 | 30 | | { |
| 21 | 31 | | if (matrix[i][j] != 0) |
| 18 | 32 | | { |
| 18 | 33 | | continue; |
| | 34 | | } |
| | 35 | |
|
| 3 | 36 | | rows[i] = true; |
| 3 | 37 | | columns[j] = true; |
| 3 | 38 | | } |
| 6 | 39 | | } |
| | 40 | |
|
| 16 | 41 | | for (var i = 0; i < rows.Length; i++) |
| 6 | 42 | | { |
| 6 | 43 | | if (!rows[i]) |
| 4 | 44 | | { |
| 4 | 45 | | continue; |
| | 46 | | } |
| | 47 | |
|
| 18 | 48 | | for (var j = 0; j < columns.Length; j++) |
| 7 | 49 | | { |
| 7 | 50 | | matrix[i][j] = 0; |
| 7 | 51 | | } |
| 2 | 52 | | } |
| | 53 | |
|
| 18 | 54 | | for (var i = 0; i < columns.Length; i++) |
| 7 | 55 | | { |
| 7 | 56 | | if (!columns[i]) |
| 4 | 57 | | { |
| 4 | 58 | | continue; |
| | 59 | | } |
| | 60 | |
|
| 24 | 61 | | for (var j = 0; j < rows.Length; j++) |
| 9 | 62 | | { |
| 9 | 63 | | matrix[j][i] = 0; |
| 9 | 64 | | } |
| 3 | 65 | | } |
| 2 | 66 | | } |
| | 67 | | } |