LeetCode Entry
1536. Minimum Swaps to Arrange a Binary Grid
Min rows swaps to zero above diagonal
1536. Minimum Swaps to Arrange a Binary Grid medium blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1285
Problem TLDR
Min rows swaps to zero above diagonal #medium
Intuition
// 62% acceptance rate, should be simple
// 200 nxn=40000
// count suffix zeros
// 0
// 1
// 2
// target is
// n-1
// n-2
// ...
// 1
// 0
// or more
// 012 210
// how to rearrange optimally?
// 201
// 210
//
// 022 220
//
// 021 210
//
// only adjucent means its a bubble sort only
//
- count suffixes
- bubble rows up
Approach
- we don’t have to add them back
Complexity
-
Time complexity: \(O(n^2)\)
-
Space complexity: \(O(n)\)
Code
// 22ms
fun minSwaps(g: Array<IntArray>) =
ArrayList(g.map { it.lastIndexOf(1) }).run {
indices.sumOf { i ->
val j = indices.find { get(it) <= i }
removeAt(j ?: return -1); j
}
}
// 0ms
pub fn min_swaps(g: Vec<Vec<i32>>) -> i32 {
let mut s = g.into_iter().map(|r|r.into_iter().rposition(|x|x>0).unwrap_or(0)).collect_vec();
(0..s.len()).try_fold(0, |r, i| {
s.iter().position(|&x| x <= i).map(|j|{ s.remove(j); r+j as i32})}).unwrap_or(-1)
}