You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2024-02-16-leetcode-73.md
+102-6
Original file line number
Diff line number
Diff line change
@@ -102,13 +102,9 @@ n == matrix[0].length
102
102
103
103
시간복잡도는 `O(n * m)` 공간복잡도는 `O(n * m)` 이다.
104
104
105
-
### in place 방식으로 접근해보기
105
+
### simple improvement `O(n + m)`
106
106
107
-
문제를 자세히 읽어보면 [in place](https://en.wikipedia.org/wiki/In-place_algorithm) 방식으로 풀라고 되어있다. 추가적인 공간을 사용하면 안된다. 그래서 다음과 같이 바꿔보았다. 컨셉은 첫번째 풀이와 동일하다.
108
-
109
-
행렬의 값은 `-231 <= matrix[i][j] <= 231 - 1` 으로 정의 되어 있다. 따라서 값을 변경해서 이미 진행된 값인지 표시하는 것은 불가능 하다.
110
-
111
-
어떤 행과 열이 0으로 바뀔지만 기록해둔다면 이전보다는 공간을 절약할 수 있다.
107
+
어떤 행과 열이 0으로 바뀔지만 기록해둔다면 이전보다 공간을 절약할 수 있다.
112
108
113
109
```java
114
110
classSolution {
@@ -154,3 +150,103 @@ n == matrix[0].length
154
150
```
155
151
156
152
시간복잡도는 `O(n * m)` 공간복잡도는 `O(n + m)` 이다. 공간 복잡도가 개선되었다.
153
+
154
+
### in place 방식
155
+
156
+
문제를 자세히 읽어보면 [in place](https://en.wikipedia.org/wiki/In-place_algorithm) 방식으로 풀라고 되어있다. 추가적인 공간을 사용하면 안된다. 그래서 다음과 같이 바꿔보았다. 컨셉은 첫번째 풀이와 동일하다.
157
+
158
+
행렬의 값은 `-231 <= matrix[i][j] <= 231 - 1` 으로 정의 되어 있다. 따라서 값을 변경해서 이미 진행된 값인지 표시하는 것은 불가능 하다.
159
+
160
+
bit를 사용해볼까도 고민해보았는데 문제에는 아래의 제약조건이 있어 포기하였다.
161
+
162
+
```
163
+
1 <= m, n <= 200
164
+
```
165
+
166
+
그러면 어떻게든 row, col 하나의 변수를 가지고 처리를 해야한다는 것이다.
167
+
168
+
힌트 3번을 보면 다음과 같은 내용이 있다.
169
+
170
+
```
171
+
We could have used 2 sets to keep a record of rows/columns which need to be set to zero. But for an O(1) space solution, you can use one of the rows and and one of the columns to keep track of this information.
172
+
```
173
+
174
+
이 힌트를 따라 다음과 같은 코드를 작성해보았다.
175
+
176
+
```java
177
+
classSolution {
178
+
publicvoidsetZeroes(int[][] matrix) {
179
+
boolean shouldUpdateFirstRow =false;
180
+
boolean shouldUpdateFirstColumn =false;
181
+
182
+
for (int i =0; i < matrix.length; i++) {
183
+
if (matrix[i][0] ==0) {
184
+
shouldUpdateFirstColumn =true;
185
+
break;
186
+
}
187
+
}
188
+
189
+
for (int j =0; j < matrix[0].length; j++) {
190
+
if (matrix[0][j] ==0) {
191
+
shouldUpdateFirstRow =true;
192
+
break;
193
+
}
194
+
}
195
+
196
+
for (int i =1; i < matrix.length; i++) {
197
+
for (int j =1; j < matrix[0].length; j++) {
198
+
if (matrix[i][j] ==0) {
199
+
matrix[i][0] =0;
200
+
matrix[0][j] =0;
201
+
}
202
+
}
203
+
}
204
+
205
+
// update row
206
+
for (int i =1; i < matrix.length; i++) {
207
+
if (matrix[i][0] ==0) {
208
+
Arrays.fill(matrix[i], 0);
209
+
}
210
+
}
211
+
212
+
// update column
213
+
for (int j =1; j < matrix[0].length; j++) {
214
+
if (matrix[0][j] ==0) {
215
+
for (int i =0; i < matrix.length; i++) {
216
+
matrix[i][j] =0;
217
+
}
218
+
}
219
+
}
220
+
221
+
// update first row if contains zero
222
+
if (shouldUpdateFirstRow) {
223
+
Arrays.fill(matrix[0], 0);
224
+
}
225
+
226
+
// update first column if contains zero
227
+
if (shouldUpdateFirstColumn) {
228
+
for (int i =0; i < matrix.length; i++) {
229
+
matrix[i][0] =0;
230
+
}
231
+
}
232
+
}
233
+
}
234
+
```
235
+
236
+
풀이 방법에 대해 설명을 하면 다음과 같다.
237
+
238
+
- 먼저는 첫번째 행과 열에 0이 있는지 미리 확인해둔다. (`shouldUpdateFirstRow`, `shouldUpdateFirstColumn`)
239
+
- 이후에는 첫번째 행과 열을 제외하고 0이 발견될 경우 첫번째 행과 열에 0으로 표시해둔다.
240
+
- 표시가 끝났으면 첫번째 행과 열을 확인하여 0일 경우 해당 줄, 열을 0으로 업데이트 한다. (단 첫번째 행과 열은 업데이트 하지 않는다., `i > 0`, `j > 0`)
241
+
- 업데이트가 끝났다면 `shouldUpdateFirstRow`, `shouldUpdateFirstColumn` 값을 확인하여 첫번째 행과 열도 0으로 업데이트 해야하는지 확인한다. 필요할 경우 0으로 업데이트 한다.
242
+
243
+
#### TC, SC
244
+
245
+
문제에서 m과 n이 다음과 같이 정의되어 있다.
246
+
247
+
```
248
+
m == matrix.length
249
+
n == matrix[0].length
250
+
```
251
+
252
+
시간복잡도는 `O(n * m)` 공간복잡도는 `O(1)` 이다. 공간 복잡도가 한 번 더 개선되었다.
0 commit comments