Skip to content

Commit 3a9386e

Browse files
committed
day 15
1 parent 249a47c commit 3a9386e

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
The K Weakest Rows in a Matrix
3+
==============================
4+
5+
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.
6+
7+
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.
8+
9+
Example 1:
10+
Input: mat =
11+
[[1,1,0,0,0],
12+
[1,1,1,1,0],
13+
[1,0,0,0,0],
14+
[1,1,0,0,0],
15+
[1,1,1,1,1]],
16+
k = 3
17+
Output: [2,0,3]
18+
Explanation:
19+
The number of soldiers for each row is:
20+
row 0 -> 2
21+
row 1 -> 4
22+
row 2 -> 1
23+
row 3 -> 2
24+
row 4 -> 5
25+
Rows ordered from the weakest to the strongest are [2,0,3,1,4]
26+
27+
Example 2:
28+
Input: mat =
29+
[[1,0,0,0],
30+
[1,1,1,1],
31+
[1,0,0,0],
32+
[1,0,0,0]],
33+
k = 2
34+
Output: [0,2]
35+
Explanation:
36+
The number of soldiers for each row is:
37+
row 0 -> 1
38+
row 1 -> 4
39+
row 2 -> 1
40+
row 3 -> 1
41+
Rows ordered from the weakest to the strongest are [0,2,3,1]
42+
43+
Constraints:
44+
m == mat.length
45+
n == mat[i].length
46+
2 <= n, m <= 100
47+
1 <= k <= m
48+
matrix[i][j] is either 0 or 1.
49+
50+
Hint #1
51+
Sort the matrix row indexes by the number of soldiers and then row indexes.
52+
*/
53+
54+
class Solution
55+
{
56+
public:
57+
vector<int> kWeakestRows(vector<vector<int>> &mat, int k)
58+
{
59+
vector<vector<int>> count;
60+
for (int i = 0; i < mat.size(); ++i)
61+
{
62+
int sum = 0;
63+
for (int j = 0; j < mat[0].size(); ++j)
64+
{
65+
sum += mat[i][j];
66+
}
67+
count.push_back({sum, i});
68+
}
69+
sort(count.begin(), count.end());
70+
vector<int> ans;
71+
for (int i = 0; i < k; ++i)
72+
ans.push_back(count[i][1]);
73+
return ans;
74+
}
75+
};

Leetcode Daily Challenge/February-2021/README.MD

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
| 7. | [Shortest Distance to a Character](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/584/week-1-february-1st-february-7th/3631/) | [cpp](./07.%20Shortest%20Distance%20to%20a%20Character.cpp) |
1212
| 8. | [Peeking Iterator](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3633/) | [cpp](./08.%20Peeking%20Iterator.cpp) |
1313
| 9. | [Convert BST to Greater Tree](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3634/) | [cpp](./09.%20Convert%20BST%20to%20Greater%20Tree.cpp) |
14-
| 10. | [Copy List with Random Pointer](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3635/) | [cpp](./10.%20Copy%20List%20with%20Random%20Pointer.cpp) |
15-
| 11. | [Valid Anagram](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3636/) | [cpp](./11.%20Valid%20Anagram.cpp) |
16-
| 12. | [Number of Steps to Reduce a Number to Zero](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3637/) | [cpp](./12.%20Number%20of%20Steps%20to%20Reduce%20a%20Number%20to%20Zero.cpp) |
17-
| 13. | [Shortest Path in Binary Matrix](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3638/) | [cpp](./13.%20Shortest%20Path%20in%20Binary%20Matrix.cpp) |
18-
| 14. | [Is Graph Bipartite?](https://leetcode.com/explore/featured/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3639/) | [cpp](./14.%20Is%20Graph%20Bipartite.cpp) |
14+
| 10. | [Copy List with Random Pointer](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3635/) | [cpp](./10.%20Copy%20List%20with%20Random%20Pointer.cpp) |
15+
| 11. | [Valid Anagram](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3636/) | [cpp](./11.%20Valid%20Anagram.cpp) |
16+
| 12. | [Number of Steps to Reduce a Number to Zero](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3637/) | [cpp](./12.%20Number%20of%20Steps%20to%20Reduce%20a%20Number%20to%20Zero.cpp) |
17+
| 13. | [Shortest Path in Binary Matrix](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3638/) | [cpp](./13.%20Shortest%20Path%20in%20Binary%20Matrix.cpp) |
18+
| 14. | [Is Graph Bipartite?](https://leetcode.com/explore/featured/card/february-leetcoding-challenge-2021/585/week-2-february-8th-february-14th/3639/) | [cpp](./14.%20Is%20Graph%20Bipartite.cpp) |
19+
| 15. | [The K Weakest Rows in a Matrix](https://leetcode.com/explore/challenge/card/february-leetcoding-challenge-2021/586/week-3-february-15th-february-21st/3641/) | [cpp](./15.%20The%20K%20Weakest%20Rows%20in%20a%20Matrix.cpp) |
1920

2021

0 commit comments

Comments
 (0)