Skip to content

Commit 4e1b683

Browse files
committed
풀이: 릿코드.1536.Minimum Swaps to Arrange a Binary Grid
?: 탐욕 알고리즘을 이용해 풀이
1 parent 92675b9 commit 4e1b683

3 files changed

Lines changed: 144 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 1536. Minimum Swaps to Arrange a Binary Grid
2+
3+
[링크](https://leetcode.com/problems/minimum-swaps-to-arrange-a-binary-grid/description/)
4+
5+
| 난이도 |
6+
| :----: |
7+
| Medium |
8+
9+
## 설계
10+
11+
### 시간 복잡도
12+
13+
2차원 배열의 크기를 N^2라 하자.
14+
15+
각 행들을 위에서 부터 순회하며 현재 채워야 하는 행으로 직접 이동한다.
16+
17+
해당 swap에 O(N)의 시간 복잡도를 사용한다. 이를 모든 행에 대해 수행하므로 O(N^2)의 시간 복잡도를 사용한다.
18+
19+
### 공간 복잡도
20+
21+
각 행에 대해 오른쪽부터 유효한 0의 갯수를 저장하는 배열을 하나 사용한다. O(N)의 공간 복잡도를 사용한다.
22+
23+
### 탐욕 알고리즘
24+
25+
| 내 코드 (ms) | 시간 복잡도 | 공간 복잡도 |
26+
| :----------: | :---------: | :---------: |
27+
| 2 | O(N^2) | O(N) |
28+
29+
각 행마다 오른쪽부터 연속된 0의 갯수로 변환 후 저장한다.
30+
31+
이후 첫번째 행부터 순회하며 조건을 만족하는 가장 가까운 행을 찾은 뒤 swap한다.
32+
33+
이를 모든 행에 대해 수행한다.
34+
35+
```cpp
36+
int minSwaps(vector<vector<int>>& grid) {
37+
int rows = grid.size();
38+
int cols = grid[0].size();
39+
40+
vector<int> counts;
41+
for (vector<int>& row : grid) {
42+
int count = 0;
43+
for (int x = cols - 1; x >= 0; x--) {
44+
if (row[x] == 0)
45+
count++;
46+
else
47+
break;
48+
}
49+
50+
counts.push_back(count);
51+
}
52+
53+
int answer = 0;
54+
for (int y = 0; y < rows; y++) {
55+
bool isFind = false;
56+
for (int i = y; i < rows; i++) {
57+
if (counts[i] >= (cols - y - 1)) {
58+
for (int j = i; j > 0; j--) {
59+
swap(counts[j], counts[j - 1]);
60+
}
61+
answer += (i - y);
62+
isFind = true;
63+
break;
64+
}
65+
}
66+
67+
if (!isFind) return -1;
68+
}
69+
return answer;
70+
}
71+
```
72+
73+
## 고생한 점
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <algorithm>
2+
#include <climits>
3+
#include <cmath>
4+
#include <cstring>
5+
#include <functional>
6+
#include <iostream>
7+
#include <map>
8+
#include <numeric>
9+
#include <queue>
10+
#include <set>
11+
#include <stack>
12+
#include <string>
13+
#include <unordered_map>
14+
#include <unordered_set>
15+
#include <vector>
16+
17+
using namespace std;
18+
19+
// use greedy
20+
// time : O(N^2)
21+
// space : O(N)
22+
class Solution {
23+
public:
24+
int minSwaps(vector<vector<int>>& grid) {
25+
int rows = grid.size();
26+
int cols = grid[0].size();
27+
28+
vector<int> counts;
29+
for (vector<int>& row : grid) {
30+
int count = 0;
31+
for (int x = cols - 1; x >= 0; x--) {
32+
if (row[x] == 0)
33+
count++;
34+
else
35+
break;
36+
}
37+
38+
counts.push_back(count);
39+
}
40+
41+
int answer = 0;
42+
for (int y = 0; y < rows; y++) {
43+
bool isFind = false;
44+
for (int i = y; i < rows; i++) {
45+
if (counts[i] >= (cols - y - 1)) {
46+
for (int j = i; j > 0; j--) {
47+
swap(counts[j], counts[j - 1]);
48+
}
49+
answer += (i - y);
50+
isFind = true;
51+
break;
52+
}
53+
}
54+
55+
if (!isFind) return -1;
56+
}
57+
return answer;
58+
}
59+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Input: grid = [[0,0,1],[1,1,0],[1,0,0]]
2+
Output: 3
3+
4+
===
5+
6+
Input: grid = [[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]]
7+
Output: -1
8+
9+
===
10+
11+
Input: grid = [[1,0,0],[1,1,0],[1,1,1]]
12+
Output: 0

0 commit comments

Comments
 (0)