You are given an m x n
binary matrix mat
of 1
s (representing soldiers) and 0
s (representing civilians). The soldiers are positioned in the front and the civilians in the back of each row. That is, all the 1
s will appear to the left of all the 0
s in each row.
A row i
is weaker than a row j
if one of the following is true:
i
is less than the number of soldiers in row j
.i < j
.Return the indices of the k
weakest rows in the matrix ordered from weakest to strongest.
m
and n
(dimensions of the matrix)?1
s and 0
s with all 1
s preceding 0
s in each row?k
rows, even if k
is larger than the number of rows?The overall strategy to solve the problem will be as follows:
Count the Soldiers: For each row in the matrix, count the number of 1’s. Since the rows are sorted with 1’s followed by 0’s, we can use binary search to find the count of 1’s efficiently.
Pair and Sort: Pair each row index with the count of soldiers and then sort these pairs. Sorting should be first by the count of soldiers and then by the row index if counts are equal.
Extract Indices: Extract and return the first k
indices from the sorted list of pairs.
Here is the implementation in Python:
from typing import List
import heapq
def kWeakestRows(mat: List[List[int]], k: int) -> List[int]:
# Function to count the number of soldiers in a row using binary search
def count_soldiers(row):
low, high = 0, len(row)
while low < high:
mid = (low + high) // 2
if row[mid] == 1:
low = mid + 1
else:
high = mid
return low
# Create a list of (number_of_soldiers, row_index) pairs
soldiers_counts = [(count_soldiers(row), idx) for idx, row in enumerate(mat)]
# Using a heap to efficiently get the k smallest elements
heapq.heapify(soldiers_counts)
# Extract the indices of the k weakest rows
return [heapq.heappop(soldiers_counts)[1] for _ in range(k)]
# Example usage
mat = [
[1, 1, 0, 0, 0],
[1, 1, 1, 1, 0],
[1, 0, 0, 0, 0],
[1, 1, 0, 0, 0],
[1, 1, 1, 1, 1]
]
k = 3
print(kWeakestRows(mat, k)) # Output: [2, 0, 3]
O(log n)
.O(m)
, where m
is the number of rows in the matrix.
Sorting the rows based on the count of soldiers takes O(m log m)
.k
smallest elements from the heap takes O(k log m)
.Overall Time Complexity: O(m log n + m log m + k log m)
In practice, m log m
is usually the dominant factor unless k
is very large.
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?