A solar farm is laid out as a grid with m rows and n columns of panels. Each panel reports 1 if it is actively generating power and 0 if it is idle. The farm's wiring activates panels within a row in a fixed left-to-right sequence, so in every row all of the active panels (1s) appear before all of the idle panels (0s) — a row can look like 1 1 1 0 0 but never 1 0 1 0 0.
The site operator wants to find the k rows producing the least power. A row's strength is the number of active panels it has. Order the rows by strength in ascending order; when two rows have the same number of active panels, the row with the smaller row index (rows are numbered from 0 starting at the top) is considered weaker. Report the indices of the k weakest rows in this order.
m and n — the number of rows and columns.m lines each contain n integers (each 0 or 1), the panel readings for that row, guaranteed to consist of a block of 1s (possibly empty) followed by a block of 0s (possibly empty).k.Print exactly k integers on one line, separated by single spaces: the 0-indexed row indices of the k weakest rows, ordered from weakest to strongest (ties broken by ascending row index).
2 <= m, n <= 1001 <= k <= m0 or 1, and within a row all 1s precede all 0s.Example 1
Input
3 4 1 1 0 0 1 1 1 1 1 0 0 0 2
Expected
2 0
Explanation
Row 0 has 2 active panels, row 1 has 4, and row 2 has 1. Sorted by active-panel count ascending (row 2 with 1, then row 0 with 2, then row 1 with 4), the 2 weakest rows are row 2 and row 0, so the answer is `2 0`.
Example 2
Input
4 3 1 0 0 1 0 0 1 1 0 0 0 0 3
Expected
3 0 1
Explanation
Row 0 and row 1 both have 1 active panel, row 2 has 2, and row 3 has 0. Sorted by count ascending with ties broken by the smaller row index first: row 3 (0), row 0 (1), row 1 (1), row 2 (2). The 3 weakest rows in order are `3 0 1`.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →