Leetcode 1337. The K Weakest Rows in a Matrix
Given a m x n
binary matrix mat
of 1
’s (representing soldiers) and 0
’s (representing civilians), return the indexes of the k
weakest rows in the matrix ordered from the weakest to the strongest.
A row i
is weaker than row j
if the number of soldiers in row i
is less than the number of soldiers in row j
, or they have the same number of soldiers but i
is less than j
. Soldiers are always standing in front of civilians, that is, all the 1
’s will appear to the left of all the 0
’s in each row.
k
is greater than the number of rows?0
’s to ensure a mixture of soldiers and civilians?1
s. Since soldiers (1
s) always appear before civilians (0
s), we can use binary search to find the first 0
for efficiency.k
weakest rows from the sorted list.Here is the implementation of the strategy in Java:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Solution {
public int[] kWeakestRows(int[][] mat, int k) {
List<int[]> soldiersCount = new ArrayList<>();
// Step 1: Count the number of soldiers in each row and pair with the row index
for (int i = 0; i < mat.length; i++) {
int soldiers = countSoldiers(mat[i]);
soldiersCount.add(new int[]{soldiers, i});
}
// Step 2: Sort the list by the number of soldiers, then by row index
Collections.sort(soldiersCount, (a, b) -> {
if (a[0] != b[0]) {
return a[0] - b[0]; // Compare by number of soldiers
} else {
return a[1] - b[1]; // Compare by row index
}
});
// Step 3: Extract the indices of the k weakest rows
int[] weakestRows = new int[k];
for (int i = 0; i < k; i++) {
weakestRows[i] = soldiersCount.get(i)[1];
}
return weakestRows;
}
// Helper method to count soldiers using binary search
private int countSoldiers(int[] row) {
int left = 0;
int right = row.length;
while (left < right) {
int mid = left + (right - left) / 2;
if (row[mid] == 1) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
}
O(m * log(n))
- counting soldiers in each of the m
rows using binary search (log(n)
).O(m * log(m))
- sorting the m
rows based on soldier count and row indices.Thus, the overall time complexity is: [ O(m \log n + m \log m) ]
This approach ensures that the matrix is processed efficiently and the weakest rows are identified correctly.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?