Skip to content

Commit 4a64cdf

Browse files
chore: add LeetCode daily solution
1 parent b342e3d commit 4a64cdf

5 files changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Minimum Swaps to Arrange a Binary Grid (Medium)
2+
3+
**Problem ID:** 1536
4+
**Date:** 2026-03-02
5+
**Link:** https://leetcode.com/problems/minimum-swaps-to-arrange-a-binary-grid/
6+
7+
## Approach
8+
9+
To solve the problem of finding the minimum number of swaps needed to arrange a binary grid such that all cells above the main diagonal are zeros, we can follow these steps:
10+
11+
### Main Idea:
12+
The key to solving this problem is to recognize that for each row in the grid, we need to determine how many zeros are at the end of the row. This will help us identify how many rows can be swapped to achieve the desired configuration.
13+
14+
### Approach:
15+
1. **Count Trailing Zeros**: For each row in the grid, count the number of trailing zeros. This count will determine how far down the row can be moved to achieve a valid configuration. Store these counts in a list.
16+
17+
2. **Determine Valid Rows**: Starting from the top row (index 0) to the bottom row (index n-1), for each row `i`, we need to ensure that there are at least `n - i - 1` zeros available in the rows below it (including itself). This is because for row `i`, we need `n - i - 1` zeros to fill the positions above the main diagonal.
18+
19+
3. **Greedy Swapping**: For each row `i`, find the first row `j` (where `j >= i`) that has enough trailing zeros to satisfy the requirement. If such a row is found, we can swap it to the `i`th position. Count the number of swaps required to bring this row to the `i`th position. If no such row exists, it means it's impossible to make the grid valid, and we return -1.
20+
21+
4. **Repeat**: Continue this process for each row until either the grid is valid or it is determined that it cannot be made valid.
22+
23+
### Data Structures:
24+
- An array (or list) to store the count of trailing zeros for each row.
25+
- A loop to iterate through the rows and perform the necessary checks and swaps.
26+
27+
### Complexity:
28+
- **Time Complexity**: O(n^2) in the worst case, where `n` is the number of rows (or columns) in the grid. This is due to the nested iteration to find rows with sufficient trailing zeros.
29+
- **Space Complexity**: O(n) for storing the counts of trailing zeros.
30+
31+
By following this structured approach, we can efficiently determine the minimum number of swaps required to make the binary grid valid or conclude that it is impossible.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public int minSwaps(int[][] grid) {
3+
int n = grid.length;
4+
int[] zerosCount = new int[n];
5+
6+
for (int i = 0; i < n; i++) {
7+
int count = 0;
8+
for (int j = n - 1; j >= 0; j--) {
9+
if (grid[i][j] == 0) {
10+
count++;
11+
} else {
12+
break;
13+
}
14+
}
15+
zerosCount[i] = count;
16+
}
17+
18+
int swaps = 0;
19+
for (int i = 0; i < n; i++) {
20+
int requiredZeros = n - 1 - i;
21+
int j = i;
22+
while (j < n && zerosCount[j] < requiredZeros) {
23+
j++;
24+
}
25+
if (j == n) {
26+
return -1;
27+
}
28+
for (int k = j; k > i; k--) {
29+
int temp = zerosCount[k];
30+
zerosCount[k] = zerosCount[k - 1];
31+
zerosCount[k - 1] = temp;
32+
swaps++;
33+
}
34+
}
35+
return swaps;
36+
}
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var minSwaps = function(grid) {
2+
const n = grid.length;
3+
const zeros = new Array(n).fill(0);
4+
5+
for (let i = 0; i < n; i++) {
6+
let count = 0;
7+
for (let j = n - 1; j >= 0; j--) {
8+
if (grid[i][j] === 0) count++;
9+
else break;
10+
}
11+
zeros[i] = count;
12+
}
13+
14+
let swaps = 0;
15+
16+
for (let i = 0; i < n; i++) {
17+
let required = n - 1 - i;
18+
let j = i;
19+
20+
while (j < n && zeros[j] < required) {
21+
j++;
22+
}
23+
24+
if (j === n) return -1;
25+
26+
for (let k = j; k > i; k--) {
27+
[zeros[k], zeros[k - 1]] = [zeros[k - 1], zeros[k]];
28+
swaps++;
29+
}
30+
}
31+
32+
return swaps;
33+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def minSwaps(self, grid: List[List[int]]) -> int:
3+
n = len(grid)
4+
zeros_needed = [0] * n
5+
6+
for i in range(n):
7+
count = 0
8+
for j in range(n):
9+
if grid[i][j] == 0:
10+
count += 1
11+
zeros_needed[i] = count
12+
13+
swaps = 0
14+
for i in range(n):
15+
if zeros_needed[i] < n - 1 - i:
16+
return -1
17+
18+
for j in range(i, n):
19+
if zeros_needed[j] >= n - 1 - i:
20+
swaps += j - i
21+
zeros_needed[i:j + 1] = [zeros_needed[j]] + zeros_needed[i:j]
22+
break
23+
24+
return swaps

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
380380
- 2026-02-27 — [Minimum Operations to Equalize Binary String](https://leetcode.com/problems/minimum-operations-to-equalize-binary-string/) (Hard) → `Hard/2026-02-27-3666-Minimum-Operations-to-Equalize-Binary-String`
381381
- 2026-02-28 — [Concatenation of Consecutive Binary Numbers](https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/) (Medium) → `Medium/2026-02-28-1680-Concatenation-of-Consecutive-Binary-Numbers`
382382
- 2026-03-01 — [Partitioning Into Minimum Number Of Deci-Binary Numbers](https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/) (Medium) → `Medium/2026-03-01-1689-Partitioning-Into-Minimum-Number-Of-Deci-Binary-Numbers`
383+
- 2026-03-02 — [Minimum Swaps to Arrange a Binary Grid](https://leetcode.com/problems/minimum-swaps-to-arrange-a-binary-grid/) (Medium) → `Medium/2026-03-02-1536-Minimum-Swaps-to-Arrange-a-Binary-Grid`

0 commit comments

Comments
 (0)