Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6aef81e

Browse files
feat: add solutions to lc problem: No.1536 (#2074)
No.1536.Minimum Swaps to Arrange a Binary Grid
1 parent 60c30be commit 6aef81e

File tree

7 files changed

+499
-2
lines changed

7 files changed

+499
-2
lines changed

‎solution/1500-1599/1536.Minimum Swaps to Arrange a Binary Grid/README.md‎

Lines changed: 173 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,194 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:贪心**
60+
61+
我们逐行处理,对于第 $i$ 行,最后一个 1ドル$ 所在的位置必须小于等于 $i,ドル我们在 $[i, n)$ 中找到第一个满足条件的行,记为 $k$。然后从第 $k$ 行开始,依次向上交换相邻的两行,直到第 $i$ 行。
62+
63+
时间复杂度 $O(n^2),ドル空间复杂度 $O(n)$。其中 $n$ 是网格的边长。
64+
5965
<!-- tabs:start -->
6066

6167
### **Python3**
6268

6369
<!-- 这里可写当前语言的特殊实现逻辑 -->
6470

6571
```python
66-
72+
class Solution:
73+
def minSwaps(self, grid: List[List[int]]) -> int:
74+
n = len(grid)
75+
pos = [-1] * n
76+
for i in range(n):
77+
for j in range(n - 1, -1, -1):
78+
if grid[i][j] == 1:
79+
pos[i] = j
80+
break
81+
ans = 0
82+
for i in range(n):
83+
k = -1
84+
for j in range(i, n):
85+
if pos[j] <= i:
86+
ans += j - i
87+
k = j
88+
break
89+
if k == -1:
90+
return -1
91+
while k > i:
92+
pos[k], pos[k - 1] = pos[k - 1], pos[k]
93+
k -= 1
94+
return ans
6795
```
6896

6997
### **Java**
7098

7199
<!-- 这里可写当前语言的特殊实现逻辑 -->
72100

73101
```java
102+
class Solution {
103+
public int minSwaps(int[][] grid) {
104+
int n = grid.length;
105+
int[] pos = new int[n];
106+
Arrays.fill(pos, -1);
107+
for (int i = 0; i < n; ++i) {
108+
for (int j = n - 1; j >= 0; --j) {
109+
if (grid[i][j] == 1) {
110+
pos[i] = j;
111+
break;
112+
}
113+
}
114+
}
115+
int ans = 0;
116+
for (int i = 0; i < n; ++i) {
117+
int k = -1;
118+
for (int j = i; j < n; ++j) {
119+
if (pos[j] <= i) {
120+
ans += j - i;
121+
k = j;
122+
break;
123+
}
124+
}
125+
if (k == -1) {
126+
return -1;
127+
}
128+
for (; k > i; --k) {
129+
int t = pos[k];
130+
pos[k] = pos[k - 1];
131+
pos[k - 1] = t;
132+
}
133+
}
134+
return ans;
135+
}
136+
}
137+
```
138+
139+
### **C++**
140+
141+
```cpp
142+
class Solution {
143+
public:
144+
int minSwaps(vector<vector<int>>& grid) {
145+
int n = grid.size();
146+
vector<int> pos(n, -1);
147+
for (int i = 0; i < n; ++i) {
148+
for (int j = n - 1; j >= 0; --j) {
149+
if (grid[i][j] == 1) {
150+
pos[i] = j;
151+
break;
152+
}
153+
}
154+
}
155+
int ans = 0;
156+
for (int i = 0; i < n; ++i) {
157+
int k = -1;
158+
for (int j = i; j < n; ++j) {
159+
if (pos[j] <= i) {
160+
ans += j - i;
161+
k = j;
162+
break;
163+
}
164+
}
165+
if (k == -1) {
166+
return -1;
167+
}
168+
for (; k > i; --k) {
169+
swap(pos[k], pos[k - 1]);
170+
}
171+
}
172+
return ans;
173+
}
174+
};
175+
```
176+
177+
### **Go**
178+
179+
```go
180+
func minSwaps(grid [][]int) (ans int) {
181+
n := len(grid)
182+
pos := make([]int, n)
183+
for i := range pos {
184+
pos[i] = -1
185+
}
186+
for i := 0; i < n; i++ {
187+
for j := n - 1; j >= 0; j-- {
188+
if grid[i][j] == 1 {
189+
pos[i] = j
190+
break
191+
}
192+
}
193+
}
194+
for i := 0; i < n; i++ {
195+
k := -1
196+
for j := i; j < n; j++ {
197+
if pos[j] <= i {
198+
ans += j - i
199+
k = j
200+
break
201+
}
202+
}
203+
if k == -1 {
204+
return -1
205+
}
206+
for ; k > i; k-- {
207+
pos[k], pos[k-1] = pos[k-1], pos[k]
208+
}
209+
}
210+
return
211+
}
212+
```
74213

214+
### **TypeScript**
215+
216+
```ts
217+
function minSwaps(grid: number[][]): number {
218+
const n = grid.length;
219+
const pos: number[] = Array(n).fill(-1);
220+
for (let i = 0; i < n; ++i) {
221+
for (let j = n - 1; ~j; --j) {
222+
if (grid[i][j] === 1) {
223+
pos[i] = j;
224+
break;
225+
}
226+
}
227+
}
228+
let ans = 0;
229+
for (let i = 0; i < n; ++i) {
230+
let k = -1;
231+
for (let j = i; j < n; ++j) {
232+
if (pos[j] <= i) {
233+
ans += j - i;
234+
k = j;
235+
break;
236+
}
237+
}
238+
if (k === -1) {
239+
return -1;
240+
}
241+
for (; k > i; --k) {
242+
[pos[k], pos[k - 1]] = [pos[k - 1], pos[k]];
243+
}
244+
}
245+
return ans;
246+
}
75247
```
76248

77249
### **...**

‎solution/1500-1599/1536.Minimum Swaps to Arrange a Binary Grid/README_EN.md‎

Lines changed: 173 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,190 @@
4646

4747
## Solutions
4848

49+
**Solution 1: Greedy**
50+
51+
We process row by row. For the $i$-th row, the position of the last '1' must be less than or equal to $i$. We find the first row that meets the condition in $[i, n),ドル denoted as $k$. Then, starting from the $k$-th row, we swap the adjacent two rows upwards until the $i$-th row.
52+
53+
The time complexity is $O(n^2),ドル and the space complexity is $O(n)$. Here, $n$ is the side length of the grid.
54+
4955
<!-- tabs:start -->
5056

5157
### **Python3**
5258

5359
```python
54-
60+
class Solution:
61+
def minSwaps(self, grid: List[List[int]]) -> int:
62+
n = len(grid)
63+
pos = [-1] * n
64+
for i in range(n):
65+
for j in range(n - 1, -1, -1):
66+
if grid[i][j] == 1:
67+
pos[i] = j
68+
break
69+
ans = 0
70+
for i in range(n):
71+
k = -1
72+
for j in range(i, n):
73+
if pos[j] <= i:
74+
ans += j - i
75+
k = j
76+
break
77+
if k == -1:
78+
return -1
79+
while k > i:
80+
pos[k], pos[k - 1] = pos[k - 1], pos[k]
81+
k -= 1
82+
return ans
5583
```
5684

5785
### **Java**
5886

5987
```java
88+
class Solution {
89+
public int minSwaps(int[][] grid) {
90+
int n = grid.length;
91+
int[] pos = new int[n];
92+
Arrays.fill(pos, -1);
93+
for (int i = 0; i < n; ++i) {
94+
for (int j = n - 1; j >= 0; --j) {
95+
if (grid[i][j] == 1) {
96+
pos[i] = j;
97+
break;
98+
}
99+
}
100+
}
101+
int ans = 0;
102+
for (int i = 0; i < n; ++i) {
103+
int k = -1;
104+
for (int j = i; j < n; ++j) {
105+
if (pos[j] <= i) {
106+
ans += j - i;
107+
k = j;
108+
break;
109+
}
110+
}
111+
if (k == -1) {
112+
return -1;
113+
}
114+
for (; k > i; --k) {
115+
int t = pos[k];
116+
pos[k] = pos[k - 1];
117+
pos[k - 1] = t;
118+
}
119+
}
120+
return ans;
121+
}
122+
}
123+
```
124+
125+
### **C++**
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
int minSwaps(vector<vector<int>>& grid) {
131+
int n = grid.size();
132+
vector<int> pos(n, -1);
133+
for (int i = 0; i < n; ++i) {
134+
for (int j = n - 1; j >= 0; --j) {
135+
if (grid[i][j] == 1) {
136+
pos[i] = j;
137+
break;
138+
}
139+
}
140+
}
141+
int ans = 0;
142+
for (int i = 0; i < n; ++i) {
143+
int k = -1;
144+
for (int j = i; j < n; ++j) {
145+
if (pos[j] <= i) {
146+
ans += j - i;
147+
k = j;
148+
break;
149+
}
150+
}
151+
if (k == -1) {
152+
return -1;
153+
}
154+
for (; k > i; --k) {
155+
swap(pos[k], pos[k - 1]);
156+
}
157+
}
158+
return ans;
159+
}
160+
};
161+
```
162+
163+
### **Go**
164+
165+
```go
166+
func minSwaps(grid [][]int) (ans int) {
167+
n := len(grid)
168+
pos := make([]int, n)
169+
for i := range pos {
170+
pos[i] = -1
171+
}
172+
for i := 0; i < n; i++ {
173+
for j := n - 1; j >= 0; j-- {
174+
if grid[i][j] == 1 {
175+
pos[i] = j
176+
break
177+
}
178+
}
179+
}
180+
for i := 0; i < n; i++ {
181+
k := -1
182+
for j := i; j < n; j++ {
183+
if pos[j] <= i {
184+
ans += j - i
185+
k = j
186+
break
187+
}
188+
}
189+
if k == -1 {
190+
return -1
191+
}
192+
for ; k > i; k-- {
193+
pos[k], pos[k-1] = pos[k-1], pos[k]
194+
}
195+
}
196+
return
197+
}
198+
```
60199

200+
### **TypeScript**
201+
202+
```ts
203+
function minSwaps(grid: number[][]): number {
204+
const n = grid.length;
205+
const pos: number[] = Array(n).fill(-1);
206+
for (let i = 0; i < n; ++i) {
207+
for (let j = n - 1; ~j; --j) {
208+
if (grid[i][j] === 1) {
209+
pos[i] = j;
210+
break;
211+
}
212+
}
213+
}
214+
let ans = 0;
215+
for (let i = 0; i < n; ++i) {
216+
let k = -1;
217+
for (let j = i; j < n; ++j) {
218+
if (pos[j] <= i) {
219+
ans += j - i;
220+
k = j;
221+
break;
222+
}
223+
}
224+
if (k === -1) {
225+
return -1;
226+
}
227+
for (; k > i; --k) {
228+
[pos[k], pos[k - 1]] = [pos[k - 1], pos[k]];
229+
}
230+
}
231+
return ans;
232+
}
61233
```
62234

63235
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /