Skip to content

Commit 5ead282

Browse files
committed
add Set Matrix Zeroes solution
1 parent 7c253cd commit 5ead282

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

set-matrix-zeroes/HoonDongKang.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* [Problem]: [73] Set Matrix Zeroes
3+
* (https://leetcode.com/problems/set-matrix-zeroes/description/)
4+
*/
5+
/**
6+
Do not return anything, modify matrix in-place instead.
7+
1. 0의 모든 행과 열을 0으로 변환
8+
2. 변경된 0과 기존 0의 차이를 만들어야 할 것 ( #으로 바꾸고 나중에 0 변환?)
9+
*/
10+
11+
function setZeroes(matrix: number[][] | string[][]): void {
12+
//시간복잡도 O(n^2)
13+
//공간복잡도 O(n)
14+
//통과는 하였지만 타입 조건을 변경해서 풀이
15+
function bruteForceFunc(matrix: number[][] | string[][]): void {
16+
const CHANGED_NUMBER = "#";
17+
const rows = matrix.length;
18+
const cols = matrix[0].length;
19+
20+
for (let i = 0; i < rows; i++) {
21+
for (let j = 0; j < cols; j++) {
22+
if (matrix[i][j] === 0) {
23+
let top = i - 1;
24+
let bottom = i + 1;
25+
let left = j - 1;
26+
let right = j + 1;
27+
28+
while (top >= 0) {
29+
if (matrix[top][j] !== 0) matrix[top][j] = CHANGED_NUMBER;
30+
top--;
31+
}
32+
while (bottom < rows) {
33+
if (matrix[bottom][j] !== 0) matrix[bottom][j] = CHANGED_NUMBER;
34+
bottom++;
35+
}
36+
while (left >= 0) {
37+
if (matrix[i][left] !== 0) matrix[i][left] = CHANGED_NUMBER;
38+
left--;
39+
}
40+
while (right < cols) {
41+
if (matrix[i][right] !== 0) matrix[i][right] = CHANGED_NUMBER;
42+
right++;
43+
}
44+
}
45+
}
46+
}
47+
48+
for (let i = 0; i < rows; i++) {
49+
for (let j = 0; j < cols; j++) {
50+
if (matrix[i][j] === CHANGED_NUMBER) matrix[i][j] = 0;
51+
}
52+
}
53+
}
54+
55+
//시간복잡도 O(m * n)
56+
//공간복잡도 O(m + n)
57+
function setFunc(matrix: number[][]): void {
58+
const rows = matrix.length;
59+
const cols = matrix[0].length;
60+
let zeroRows = new Set<number>();
61+
let zeroCols = new Set<number>();
62+
63+
for (let i = 0; i < rows; i++) {
64+
for (let j = 0; j < cols; j++) {
65+
if (matrix[i][j] === 0) {
66+
zeroCols.add(j);
67+
zeroRows.add(i);
68+
}
69+
}
70+
}
71+
72+
for (let r of zeroRows) {
73+
for (let i = 0; i < cols; i++) {
74+
matrix[r][i] = 0;
75+
}
76+
}
77+
78+
for (let c of zeroCols) {
79+
for (let i = 0; i < rows; i++) {
80+
matrix[i][c] = 0;
81+
}
82+
}
83+
}
84+
85+
//시간복잡도 O(m * n)
86+
//공간복잡도 O(1)
87+
function optimizeSpaceFunc(matrix: number[][]): void {
88+
const rows = matrix.length;
89+
const cols = matrix[0].length;
90+
let isFirstRowZero = false;
91+
let isFirstColZero = false;
92+
93+
for (let i = 0; i < rows; i++) {
94+
if (matrix[i][0] === 0) isFirstColZero = true;
95+
}
96+
97+
for (let j = 0; j < cols; j++) {
98+
if (matrix[0][j] === 0) isFirstRowZero = true;
99+
}
100+
101+
for (let i = 1; i < rows; i++) {
102+
for (let j = 1; j < cols; j++) {
103+
if (matrix[i][j] === 0) {
104+
matrix[i][0] = 0;
105+
matrix[0][j] = 0;
106+
}
107+
}
108+
}
109+
110+
for (let i = 1; i < rows; i++) {
111+
for (let j = 1; j < cols; j++) {
112+
if (matrix[i][0] === 0 || matrix[0][j] === 0) {
113+
matrix[i][j] = 0;
114+
}
115+
}
116+
}
117+
118+
if (isFirstColZero) {
119+
for (let i = 0; i < rows; i++) {
120+
matrix[i][0] = 0;
121+
}
122+
}
123+
124+
if (isFirstRowZero) {
125+
for (let j = 0; j < cols; j++) {
126+
matrix[0][j] = 0;
127+
}
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)