1337. The K Weakest Rows in a Matrix Easy

@problem@discussion
#Array#Binary Search#Sorting#Heap (Priority Queue)#Matrix



1/**
2 * [1337] The K Weakest Rows in a Matrix
3 *
4 * 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 front of the civilians. That is, all the 1's will appear to the left of all the 0's in each row.
5 * A row i is weaker than a row j if one of the following is true:
6 * 
7 * 	The number of soldiers in row i is less than the number of soldiers in row j.
8 * 	Both rows have the same number of soldiers and i < j.
9 * 
10 * Return the indices of the k weakest rows in the matrix ordered from weakest to strongest.
11 *  
12 * Example 1:
13 * 
14 * Input: mat = 
15 * [[1,1,0,0,0],
16 *  [1,1,1,1,0],
17 *  [1,0,0,0,0],
18 *  [1,1,0,0,0],
19 *  [1,1,1,1,1]], 
20 * k = 3
21 * Output: [2,0,3]
22 * Explanation: 
23 * The number of soldiers in each row is: 
24 * - Row 0: 2 
25 * - Row 1: 4 
26 * - Row 2: 1 
27 * - Row 3: 2 
28 * - Row 4: 5 
29 * The rows ordered from weakest to strongest are [2,0,3,1,4].
30 * 
31 * Example 2:
32 * 
33 * Input: mat = 
34 * [[1,0,0,0],
35 *  [1,1,1,1],
36 *  [1,0,0,0],
37 *  [1,0,0,0]], 
38 * k = 2
39 * Output: [0,2]
40 * Explanation: 
41 * The number of soldiers in each row is: 
42 * - Row 0: 1 
43 * - Row 1: 4 
44 * - Row 2: 1 
45 * - Row 3: 1 
46 * The rows ordered from weakest to strongest are [0,2,3,1].
47 * 
48 *  
49 * Constraints:
50 * 
51 * 	m == mat.length
52 * 	n == mat[i].length
53 * 	2 <= n, m <= 100
54 * 	1 <= k <= m
55 * 	matrix[i][j] is either 0 or 1.
56 * 
57 */
58pub struct Solution {}
59
60// problem: https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/
61// discuss: https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/?currentPage=1&orderBy=most_votes&query=
62
63// submission codes start here
64
65impl Solution {
66    pub fn k_weakest_rows(mat: Vec<Vec<i32>>, k: i32) -> Vec<i32> {
67        vec![]
68    }
69}
70
71// submission codes end
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn test_1337() {
79    }
80}
81


Back
© 2025 bowen.ge All Rights Reserved.