Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

The K Weakest Rows in a Matrix

XPChallenge Points: 10
levelLevel: Easy

You’re given an m × n binary matrix mat where each row has all 1s (soldiers) to the left of all 0s (civilians).
Row i is weaker than row j if:

  1. it has fewer soldiers, or

  2. they have the same number of soldiers and i < j.

Return the indices of the k weakest rows, ordered from weakest to strongest.

Example 1:

Input: 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

Output: [2,0,3]

Explanation:

Soldiers per row: [2,4,1,2,5] Order: [2,0,3,1,4] → first 3 = [2,0,3].

Example 2:

Input: mat = [ [1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0] ], k = 2

Output: [0,2]

Explanation:

Soldiers per row: [1,4,1,1] Order: [0,2,3,1] → first 2 = [0,2].

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result