Skip to content

Conversation

@BangDori
Copy link
Member

@BangDori BangDori commented Jan 1, 2025

문제

Type Info
Time Complexity O(N * M)
Space Complexity O(K)
Algorithm X
Data Structure Set

Constraints

  • m == matrix.length
  • n == matrix[0].length
  • 1 <= m, n <= 200
  • -231 <= matrix[i][j] <= 231 - 1

Edge Case

  1. 반례 케이스 (겹치는 부분이 존재하는 경우)
1 2 3 4
5 0 7 8
0 10 11 12
13 14 15 0

풀이

  1. matrix 배열에서 0 값을 가지는 row, col을 각각 따로 zeroRows, zeroCols set에 저장한다.
  2. zeroRows 집합을 순회하면서 zeroRow에 해당하는 모든 col에 대해 0으로 수정한다.
  3. zeroCols 집합을 순회하면서 zeroCol에 해당하는 모든 row에 대해 0으로 수정한다.

어려웠던 점

DFS로 문제 풀이 방법을 정하고 문제 풀이를 시도해보니 코드가 어느정도 완성된 상태에서 반례 케이스에 대한 대응이 어려웠다.

알게된 점

  • 문제를 해결하기 위해 알고리즘을 먼저 선택하는 것이 아닌, 반례 케이스에 대해 우선적으로 고려해보고 현재 적용해볼 수 있는 알고리즘에 대해 생각해보아야겠다.

Comment on lines +12 to +19
for (let row = 0; row < rowSize; row++) {
for (let col = 0; col < colSize; col++) {
if (matrix[row][col] !== 0) continue;

zeroRows.add(row);
zeroCols.add(col);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 if (matrix[row][col] === 0){
      zeroRows.add(row);
      zeroCols.add(col);
}

로 안하고 !==으로 표현한건 가독성 때문인건가요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 맞습니다

해당 문제에서 0인 지점에 대해서만, 해당 지점을 기준으로 행과 열을 0으로 초기화하라고 했기 때문에 0이 아닌 경우를 무시하도록 하였습니다.

@github-actions github-actions bot enabled auto-merge (squash) January 9, 2025 01:24
@github-actions github-actions bot merged commit 87ea5f5 into main Jan 9, 2025
2 checks passed
@BangDori BangDori deleted the bangdori/set-matrix-zeroes branch January 10, 2025 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants