Skip to content

Commit 23cfd49

Browse files
authored
[ PS ] : Set Matrix Zeroes
1 parent f46290a commit 23cfd49

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* ์ฃผ์–ด์ง„ ๊ฒฉ์ž์—์„œ ์›์†Œ๊ฐ€ 0์ธ ํ–‰๊ณผ ์—ด์˜ ๊ฐ’์„ 0์œผ๋กœ ์ˆ˜์ •ํ•˜๋Š” ํ•จ์ˆ˜
3+
* @param {number[][]} matrix
4+
* @return {void} Do not return anything, modify matrix in-place instead.
5+
*/
6+
const setZeroes = function (matrix) {
7+
const m = matrix.length;
8+
const n = matrix[0].length;
9+
const rows = new Set();
10+
const cols = new Set();
11+
12+
for (let r = 0; r < m; r++) {
13+
for (let c = 0; c < n; c++) {
14+
if (matrix[r][c] === 0) {
15+
rows.add(r);
16+
cols.add(c);
17+
}
18+
}
19+
}
20+
21+
rows.forEach((row) => matrix[row] = Array(n).fill(0));
22+
cols.forEach((col) => {
23+
for (let r = 0; r < m; r++) {
24+
matrix[r][col] = 0;
25+
}
26+
});
27+
};
28+
29+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(m * n)
30+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(m + n)

0 commit comments

Comments
ย (0)