Skip to content

Commit a9c5c8e

Browse files
add 1727
1 parent 3a62bc5 commit a9c5c8e

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1727|[Largest Submatrix With Rearrangements](https://leetcode.com/problems/largest-submatrix-with-rearrangements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1727.java) ||Medium|Greedy, Sort|
1112
|1726|[Tuple with Same Product](https://leetcode.com/problems/tuple-with-same-product/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1726.java) ||Medium|Array|
1213
|1725|[Number Of Rectangles That Can Form The Largest Square](https://leetcode.com/problems/number-of-rectangles-that-can-form-the-largest-square/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1725.java) ||Easy|Greedy|
1314
|1721|[Swapping Nodes in a Linked List](https://leetcode.com/problems/swapping-nodes-in-a-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1721.java) ||Medium|LinkedList|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
public class _1727 {
6+
public static class Solution1 {
7+
/**
8+
* credit: https://leetcode.com/problems/largest-submatrix-with-rearrangements/discuss/1020682/Java-or-6ms-or-easy-understanding-with-comments-and-images
9+
*/
10+
public int largestSubmatrix(int[][] matrix) {
11+
int m = matrix.length;
12+
int n = matrix[0].length;
13+
for (int i = 1; i < m; i++) {
14+
for (int j = 0; j < n; j++) {
15+
if (matrix[i][j] != 0) {
16+
matrix[i][j] = matrix[i - 1][j] + 1;
17+
}
18+
}
19+
}
20+
int count = 0;
21+
for (int i = 0; i < m; i++) {
22+
Arrays.sort(matrix[i]);
23+
for (int j = 1; j <= n; j++) {
24+
count = Math.max(count, j * matrix[i][n - j]);
25+
}
26+
}
27+
return count;
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1;
5+
import com.fishercoder.solutions._1727;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertArrayEquals;
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _1727Test {
13+
private static _1727.Solution1 solution1;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _1727.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
assertEquals(8, solution1.largestSubmatrix(
23+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,1,1,1,1,1,1],[1,1,0,1,1,0,1],[1,0,0,1,0,1,1]")));
24+
}
25+
26+
}

0 commit comments

Comments
 (0)