Leetcode – The K Weakest Rows in a Matrix Solution

Spread the love

The K Weakest Rows in a Matrix is an intriguing problem on Leetcode that’s often used to assess algorithmic aptitude in technical interviews. This problem encompasses various skills including array manipulation, matrix traversal, and sorting algorithms. In this exhaustive guide, we will dive deep into the problem, its constraints, and the different strategies that can be employed to solve it, along with their time and space complexities.

Problem Statement

The problem is formally described as follows:

Given a m * n matrix mat of ones (representing soldiers) and zeros (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 stand in the frontier of a row, that is, always ones may appear first and then zeros.


Input: mat = 
k = 3
Output: [2,0,3]

Approaches to Solve the Problem

Brute-force Approach

The most straightforward approach is to count the number of soldiers in each row, store this information along with the row index, and then sort this array to find the k weakest rows.

Here’s a Python code snippet that demonstrates this:

def kWeakestRows(mat, k):
    row_strength = []
    for i, row in enumerate(mat):
        row_strength.append((sum(row), i))
    return [index for _, index in row_strength[:k]]

Time Complexity: O(m×n) for calculating row strengths, O(m log ⁡m) for sorting.
Space Complexity: O(m)

Priority Queue Approach

We can use a priority queue to keep track of the k weakest rows as we go through the matrix. This allows us to avoid sorting the entire array of row strengths, thus potentially reducing the computational overhead.

Here is the Python code for this approach:

import heapq

def kWeakestRows(mat, k):
    min_heap = []
    for i, row in enumerate(mat):
        strength = sum(row)
        heapq.heappush(min_heap, (-strength, -i))
        if len(min_heap) > k:
    weakest = []
    while min_heap:
        strength, i = heapq.heappop(min_heap)
    return reversed(weakest)

Time Complexity: O(m×n) for calculating row strengths, O(m log ⁡k) for maintaining the heap.
Space Complexity: O(k)

Why These Approaches Work

Both approaches rely on the fundamental idea of counting the soldiers in each row to determine the “strength” of that row. The difference lies in how they store and sort this information to find the k weakest rows efficiently.

The brute-force approach calculates the strength of all rows and sorts them all, which is a straightforward but not the most efficient way.

The priority queue approach optimizes this by keeping only the k weakest rows in the heap at any given time. This way, we reduce the space complexity and potentially make the sorting step more efficient.

Edge Cases

  1. All rows have the same strength: In this case, the function should return the first k indices.
  2. Some rows have the same strength: The function should prioritize the row that comes first.


The “K Weakest Rows in a Matrix” problem serves as an excellent exercise for understanding matrix manipulation, sorting algorithms, and optimization techniques like priority queues. The problem challenges you to think about the most efficient way to find the k weakest rows, providing a platform to test not just your coding skills but also your algorithmic thinking.

While the problem has a relatively simple brute-force solution, delving deeper to understand and implement the optimized solution can provide valuable insights into the nuances of algorithm optimization.

Leave a Reply