Skip to content

Commit 857c359

Browse files
add 1341
1 parent eaad3dc commit 857c359

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ _If you like this project, please leave me a star._ ★
99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
1111
|1343|[Maximum Product of Splitted Binary Tree](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1343.java) | |Medium||
12+
|1341|[The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1341.java) | |Easy||
1213
|1333|[Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1333.java) | |Medium||
1314
|1331|[Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1331.java) | |Easy||
1415
|1329|[Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1329.java) | |Medium||
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
/**
8+
* 1341. The K Weakest Rows in a Matrix
9+
*
10+
* Given a m * n matrix mat of ones (representing soldiers) and zeros (representing civilians),
11+
* return the indexes of the k weakest rows in the matrix ordered from the weakest to the strongest.
12+
* 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,
13+
* or they have the same number of soldiers but i is less than j. Soldiers are always stand in the frontier of a row,
14+
* that is, always ones may appear first and then zeros.
15+
*
16+
* Example 1:
17+
* Input: mat =
18+
* [[1,1,0,0,0],
19+
* [1,1,1,1,0],
20+
* [1,0,0,0,0],
21+
* [1,1,0,0,0],
22+
* [1,1,1,1,1]],
23+
* k = 3
24+
* Output: [2,0,3]
25+
* Explanation:
26+
* The number of soldiers for each row is:
27+
* row 0 -> 2
28+
* row 1 -> 4
29+
* row 2 -> 1
30+
* row 3 -> 2
31+
* row 4 -> 5
32+
* Rows ordered from the weakest to the strongest are [2,0,3,1,4]
33+
*
34+
* Example 2:
35+
* Input: mat =
36+
* [[1,0,0,0],
37+
* [1,1,1,1],
38+
* [1,0,0,0],
39+
* [1,0,0,0]],
40+
* k = 2
41+
* Output: [0,2]
42+
* Explanation:
43+
* The number of soldiers for each row is:
44+
* row 0 -> 1
45+
* row 1 -> 4
46+
* row 2 -> 1
47+
* row 3 -> 1
48+
* Rows ordered from the weakest to the strongest are [0,2,3,1]
49+
*
50+
* Constraints:
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+
public class _1341 {
58+
public static class Solution1 {
59+
public int[] kWeakestRows(int[][] mat, int k) {
60+
List<int[]> list = new ArrayList<>();
61+
for (int i = 0; i < mat.length; i++) {
62+
int soldiers = 0;
63+
for (int j = 0; j < mat[0].length; j++) {
64+
if (mat[i][j] == 1) {
65+
soldiers++;
66+
} else {
67+
break;
68+
}
69+
}
70+
list.add(new int[]{i, soldiers});
71+
}
72+
Collections.sort(list, (a, b) -> a[1] == b[1] ? a[0] - b[0] : a[1] - b[1]);
73+
int[] result = new int[k];
74+
int i = 0;
75+
while (i < k) {
76+
result[i] = list.get(i++)[0];
77+
}
78+
return result;
79+
}
80+
}
81+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1341;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1341Test {
10+
private static _1341.Solution1 solution1;
11+
private static int[][] mat;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1341.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
mat = new int[][]{
21+
{1, 1, 0, 0, 0},
22+
{1, 1, 1, 1, 0},
23+
{1, 0, 0, 0, 0},
24+
{1, 1, 0, 0, 0},
25+
{1, 1, 1, 1, 1}
26+
};
27+
assertArrayEquals(new int[]{2, 0}, solution1.kWeakestRows(mat, 2));
28+
}
29+
30+
@Test
31+
public void test2() {
32+
mat = new int[][]{
33+
{1, 0, 0, 0},
34+
{1, 1, 1, 1},
35+
{1, 0, 0, 0},
36+
{1, 0, 0, 0}
37+
};
38+
assertArrayEquals(new int[]{0, 2}, solution1.kWeakestRows(mat, 2));
39+
}
40+
41+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy