Skip to content

Commit b1f9999

Browse files
add solutions for 1277
1 parent 933b66e commit b1f9999

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ _If you like this project, please leave me a star._ ★
1010
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
1111
|1282|[Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1282.java) | | | |Medium||
1212
|1281|[Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1281.java) | | | |Easy||
13+
|1277|[Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1277.java) | | | |Medium||
1314
|1266|[Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1266.java) | | | |Easy||
1415
|1260|[Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1260.java) | | | [:tv:](https://www.youtube.com/watch?v=9hBcARSiU0s)|Easy||
1516
|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java) | O(m*n + k) | O(m*n) | |Easy||

src/main/java/com/fishercoder/solutions/_1277.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,60 @@
3939
* */
4040
public class _1277 {
4141
public static class Solution1 {
42+
/**
43+
* In-place solution.
44+
*
45+
* credit: https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/441306/Python-DP-solution
46+
* */
4247
public int countSquares(int[][] matrix) {
48+
int m = matrix.length;
49+
int n = matrix[0].length;
4350
int count = 0;
51+
for (int i = 0; i < m; i++) {
52+
for (int j = 0; j < n; j++) {
53+
if (matrix[i][j] > 0 && i > 0 && j > 0) {
54+
matrix[i][j] = Math.min(matrix[i - 1][j - 1], Math.min(matrix[i - 1][j], matrix[i][j - 1])) + 1;
55+
}
56+
count += matrix[i][j];
57+
}
58+
}
59+
return count;
60+
}
61+
}
62+
63+
public static class Solution2 {
64+
/**
65+
* Use m*n extra space solution.
66+
*
67+
* credit: https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/441312/Java-Simple-DP-solution
68+
*/
69+
public int countSquares(int[][] matrix) {
70+
int m = matrix.length;
71+
int n = matrix[0].length;
72+
int[][] dp = new int[m][n];
73+
for (int i = 0; i < m; i++) {
74+
if (matrix[i][0] == 1) {
75+
dp[i][0] = 1;
76+
}
77+
}
78+
for (int j = 0; j < n; j++) {
79+
if (matrix[0][j] == 1) {
80+
dp[0][j] = 1;
81+
}
82+
}
83+
for (int i = 1; i < m; i++) {
84+
for (int j = 1; j < n; j++) {
85+
if (matrix[i][j] == 1) {
86+
dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1;
87+
}
88+
}
89+
}
90+
int count = 0;
91+
for (int i = 0; i < m; i++) {
92+
for (int j = 0; j < n; j++) {
93+
count += dp[i][j];
94+
}
95+
}
4496
return count;
4597
}
4698
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1277;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1277Test {
10+
private static _1277.Solution1 solution1;
11+
private static _1277.Solution2 solution2;
12+
private static int[][] matrix;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1277.Solution1();
17+
solution2 = new _1277.Solution2();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
matrix = new int[][]{
23+
{0, 1, 1, 1},
24+
{1, 1, 1, 1},
25+
{0, 1, 1, 1}
26+
};
27+
assertEquals(15, solution1.countSquares(matrix));
28+
}
29+
30+
@Test
31+
public void test2() {
32+
matrix = new int[][]{
33+
{0, 1, 1, 1},
34+
{1, 1, 1, 1},
35+
{0, 1, 1, 1}
36+
};
37+
assertEquals(15, solution2.countSquares(matrix));
38+
}
39+
40+
}

0 commit comments

Comments
 (0)