Skip to content

Commit 4fd476e

Browse files
add 3212
1 parent e0c14fc commit 4fd476e

File tree

2 files changed

+50
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src/main/java/com/fishercoder/solutions/fourththousand

2 files changed

+50
-0
lines changed

paginated_contents/algorithms/4th_thousand/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3212 | [Count Submatrices With Equal Frequency of X and Y](https://leetcode.com/problems/count-submatrices-with-equal-frequency-of-x-and-y/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3212.java) | | Medium | Prefix Sum
34
| 3211 | [Generate Binary Strings Without Adjacent Zeros](https://leetcode.com/problems/generate-binary-strings-without-adjacent-zeros/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3211.java) | | Medium | Recursion, Backtracking
45
| 3210 | [Find the Encrypted String](https://leetcode.com/problems/find-the-encrypted-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3210.java) | | Easy |
56
| 3208 | [Alternating Groups II](https://leetcode.com/problems/alternating-groups-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3208.java) | | Medium |
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
public class _3212 {
4+
public static class Solution1 {
5+
/**
6+
* My completely original solution: (although it could be further optimized.)
7+
* use a 3-d array, dp[i][j][0] means the number of x's and dp[i][j][1] means the number of y's startring from (0,0) all the way to (i,j)
8+
* then how to compute prefix sum:
9+
* I used two steps in sequence:
10+
* first: I calculate the number of x's and y's for each row;
11+
* second: I sum up both x's and y's from its previous row with its current row
12+
*/
13+
public int numberOfSubmatrices(char[][] grid) {
14+
int count = 0;
15+
int m = grid.length;
16+
int n = grid[0].length;
17+
int[][][] dp = new int[m][n][2];
18+
for (int i = 0; i < m; i++) {
19+
for (int j = 0; j < n; j++) {
20+
if (grid[i][j] == 'X') {
21+
dp[i][j][0]++;
22+
for (int k = j + 1; k < n; k++) {
23+
dp[i][k][0]++;
24+
}
25+
} else if (grid[i][j] == 'Y') {
26+
dp[i][j][1]++;
27+
for (int k = j + 1; k < n; k++) {
28+
dp[i][k][1]++;
29+
}
30+
}
31+
}
32+
}
33+
for (int i = 1; i < m; i++) {
34+
for (int j = 0; j < n; j++) {
35+
dp[i][j][0] += dp[i - 1][j][0];
36+
dp[i][j][1] += dp[i - 1][j][1];
37+
}
38+
}
39+
for (int i = 0; i < m; i++) {
40+
for (int j = 0; j < n; j++) {
41+
if (dp[i][j][0] != 0 && dp[i][j][0] == dp[i][j][1]) {
42+
count++;
43+
}
44+
}
45+
}
46+
return count;
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)