java.lang.Object
g1301_1400.s1337_the_k_weakest_rows_in_a_matrix.Solution

public class Solution extends Object
1337 - The K Weakest Rows in a Matrix.<p>Easy</p> <p>You are given an <code>m x n</code> binary matrix <code>mat</code> of <code>1</code>&rsquo;s (representing soldiers) and <code>0</code>&rsquo;s (representing civilians). The soldiers are positioned <strong>in front</strong> of the civilians. That is, all the <code>1</code>&rsquo;s will appear to the <strong>left</strong> of all the <code>0</code>&rsquo;s in each row.</p> <p>A row <code>i</code> is <strong>weaker</strong> than a row <code>j</code> if one of the following is true:</p> <ul> <li>The number of soldiers in row <code>i</code> is less than the number of soldiers in row <code>j</code>.</li> <li>Both rows have the same number of soldiers and <code>i < j</code>.</li> </ul> <p>Return <em>the indices of the</em> <code>k</code> <em><strong>weakest</strong> rows in the matrix ordered from weakest to strongest</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> mat =</p> <p>[[1,1,0,0,0],</p> <p>[1,1,1,1,0],</p> <p>[1,0,0,0,0],</p> <p>[1,1,0,0,0],</p> <p>[1,1,1,1,1]], k = 3</p> <p><strong>Output:</strong> [2,0,3]</p> <p><strong>Explanation:</strong> The number of soldiers in each row is:</p> <ul> <li>Row 0: 2</li> <li>Row 1: 4</li> <li>Row 2: 1</li> <li>Row 3: 2</li> <li>Row 4: 5</li> </ul> <p>The rows ordered from weakest to strongest are [2,0,3,1,4].</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]], k = 2</p> <p><strong>Output:</strong> [0,2]</p> <p><strong>Explanation:</strong> The number of soldiers in each row is:</p> <ul> <li>Row 0: 1</li> <li>Row 1: 4</li> <li>Row 2: 1</li> <li>Row 3: 1</li> </ul> <p>The rows ordered from weakest to strongest are [0,2,3,1].</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>2 <= n, m <= 100</code></li> <li><code>1 <= k <= m</code></li> <li><code>matrix[i][j]</code> is either 0 or 1.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • kWeakestRows

      public int[] kWeakestRows(int[][] mat, int k)