|
| 1 | +/** |
| 2 | + Do not return anything, modify matrix in-place instead. |
| 3 | + */ |
| 4 | +/** |
| 5 | + * @link https://leetcode.com/problems/set-matrix-zeroes/description/ |
| 6 | + * |
| 7 | + * ์ ๊ทผ ๋ฐฉ๋ฒ : |
| 8 | + * - ํ๋ ฌ ์ํํ๋ฉด์ 0์ด ์๋ ํ๊ณผ ์ด์ rowsToZero๊ณผ colsToZero์ ์ ์ฅ |
| 9 | + * - ๋ค์ ํ๋ ฌ ์ํํ๋ฉด์ rowsToZero๊ณผ colsToZero์ ํฌํจ๋ ๊ฒฝ์ฐ 0์ผ๋ก ๋ณ๊ฒฝ |
| 10 | + * |
| 11 | + * ์๊ฐ๋ณต์ก๋ : O(m * n) |
| 12 | + * - m * n ํ๋ ฌ ํฌ๊ธฐ๋งํผ ์ํ |
| 13 | + * |
| 14 | + * ๊ณต๊ฐ๋ณต์ก๋ : O(m + n) |
| 15 | + * - m ํ๊ณผ n ์ด ์ ๋ณด ์ ์ฅ |
| 16 | + */ |
| 17 | +function setZeroes(matrix: number[][]): void { |
| 18 | + const rows = matrix.length; |
| 19 | + const cols = matrix[0].length; |
| 20 | + const rowsToZero = new Set<number>(); |
| 21 | + const colsToZero = new Set<number>(); |
| 22 | + for (let row = 0; row < rows; row++) { |
| 23 | + for (let col = 0; col < cols; col++) { |
| 24 | + if (matrix[row][col] === 0) { |
| 25 | + rowsToZero.add(row); |
| 26 | + colsToZero.add(col); |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + for (let row = 0; row < rows; row++) { |
| 32 | + for (let col = 0; col < cols; col++) { |
| 33 | + if (rowsToZero.has(row) || colsToZero.has(col)) { |
| 34 | + matrix[row][col] = 0; |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// ๊ณต๊ฐ ๋ณต์ก๋ O(m+n)์ O(1)๋ก ๊ฐ์ ํ๊ธฐ |
| 41 | +// set์ ํ๊ณผ ์ด ์ ๋ณด ์ ์ฅํ๋ ๋ฐฉ์์ set์์ด ์ฒซ ๋ฒ์งธ ํ๊ณผ ์ด์ ํ์ฉํ๋ ๋ฐฉ๋ฒ์ผ๋ก ๋ณ๊ฒฝ |
| 42 | +function setZeroes(matrix: number[][]): void { |
| 43 | + const rows = matrix.length; |
| 44 | + const cols = matrix[0].length; |
| 45 | + let hasZeroInFirstRow = false; |
| 46 | + let hasZeroInFirstCol = false; |
| 47 | + |
| 48 | + // ์ฒซ ๋ฒ์งธ ์ด์ 0 ์๋์ง ์ฌ๋ถ๋ฅผ ํ๋๊ทธ๋ก ์ ์ฅ |
| 49 | + for (let row = 0; row < rows; row++) { |
| 50 | + if (matrix[row][0] === 0) hasZeroInFirstCol = true; |
| 51 | + } |
| 52 | + |
| 53 | + // ์ฒซ ๋ฒ์งธ ํ์ 0 ์๋์ง ์ฌ๋ถ๋ฅผ ํ๋๊ทธ๋ก ์ ์ฅ |
| 54 | + for (let col = 0; col < cols; col++) { |
| 55 | + if (matrix[0][col] === 0) hasZeroInFirstRow = true; |
| 56 | + } |
| 57 | + |
| 58 | + // ์ฒซ ๋ฒ์งธ ํ๊ณผ ์ด ์ ์ธํ๊ณ ๋ง์ปค ์ค์ ํ๊ธฐ |
| 59 | + for (let row = 1; row < rows; row++) { |
| 60 | + for (let col = 1; col < cols; col++) { |
| 61 | + if (matrix[row][col] === 0) { |
| 62 | + matrix[0][col] = 0; |
| 63 | + matrix[row][0] = 0; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // ๋ง์ปค ๊ธฐ๋ฐ์ผ๋ก ํ๋ ฌ์ ๋ฐ์ |
| 69 | + for (let row = 1; row < rows; row++) { |
| 70 | + for (let col = 1; col < cols; col++) { |
| 71 | + if (matrix[row][0] === 0 || matrix[0][col] === 0) matrix[row][col] = 0; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // ์ฒซ ๋ฒ์งธ ํ ์
๋ฐ์ดํธ |
| 76 | + if (hasZeroInFirstRow) { |
| 77 | + for (let col = 0; col < cols; col++) { |
| 78 | + matrix[0][col] = 0; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // ์ฒซ ๋ฒ์งธ ์ด ์
๋ฐ์ดํธ |
| 83 | + if (hasZeroInFirstCol) { |
| 84 | + for (let row = 0; row < rows; row++) { |
| 85 | + matrix[row][0] = 0; |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments