题目描述
给你一个二维字符矩阵 grid
,其中 grid[i][j]
可能是 'X'
、'Y'
或 '.'
,返回满足以下条件的子矩阵数量:
- 包含
grid[0][0]
'X'
和 'Y'
的频数相等。
- 至少包含一个
'X'
。
示例 1:
输入: grid = [["X","Y","."],["Y",".","."]]
输出: 3
解释:
示例 2:
输入: grid = [["X","X"],["X","Y"]]
输出: 0
解释:
不存在满足 'X'
和 'Y'
频数相等的子矩阵。
示例 3:
输入: grid = [[".","."],[".","."]]
输出: 0
解释:
不存在满足至少包含一个 'X'
的子矩阵。
提示:
1 <= grid.length, grid[i].length <= 1000
grid[i][j]
可能是 'X'
、'Y'
或 '.'
.
解法
方法一:二维前缀和
根据题目描述,我们只需要统计每个位置 $(i, j)$ 的前缀和 $s[i][j][0]$ 和 $s[i][j][1]$,分别表示从 $(0, 0)$ 到 $(i, j)$ 的子矩阵中字符 X
和 Y
的数量,如果 $s[i][j][0] > 0$ 且 $s[i][j][0] = s[i][j][1]$,则说明满足题目条件,答案加一。
遍历完所有位置后,返回答案即可。
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别表示矩阵的行数和列数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | class Solution:
def numberOfSubmatrices(self, grid: List[List[str]]) -> int:
m, n = len(grid), len(grid[0])
s = [[[0] * 2 for _ in range(n + 1)] for _ in range(m + 1)]
ans = 0
for i, row in enumerate(grid, 1):
for j, x in enumerate(row, 1):
s[i][j][0] = s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0]
s[i][j][1] = s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1]
if x != ".":
s[i][j][ord(x) & 1] += 1
if s[i][j][0] > 0 and s[i][j][0] == s[i][j][1]:
ans += 1
return ans
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | class Solution {
public int numberOfSubmatrices(char[][] grid) {
int m = grid.length, n = grid[0].length;
int[][][] s = new int[m + 1][n + 1][2];
int ans = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
s[i][j][0] = s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0]
+ (grid[i - 1][j - 1] == 'X' ? 1 : 0);
s[i][j][1] = s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1]
+ (grid[i - 1][j - 1] == 'Y' ? 1 : 0);
if (s[i][j][0] > 0 && s[i][j][0] == s[i][j][1]) {
++ans;
}
}
}
return ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | class Solution {
public:
int numberOfSubmatrices(vector<vector<char>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<vector<vector<int>>> s(m + 1, vector<vector<int>>(n + 1, vector<int>(2)));
int ans = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
s[i][j][0] = s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0]
+ (grid[i - 1][j - 1] == 'X' ? 1 : 0);
s[i][j][1] = s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1]
+ (grid[i - 1][j - 1] == 'Y' ? 1 : 0);
if (s[i][j][0] > 0 && s[i][j][0] == s[i][j][1]) {
++ans;
}
}
}
return ans;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 | func numberOfSubmatrices(grid [][]byte) (ans int) {
m, n := len(grid), len(grid[0])
s := make([][][]int, m+1)
for i := range s {
s[i] = make([][]int, n+1)
for j := range s[i] {
s[i][j] = make([]int, 2)
}
}
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
s[i][j][0] = s[i-1][j][0] + s[i][j-1][0] - s[i-1][j-1][0]
if grid[i-1][j-1] == 'X' {
s[i][j][0]++
}
s[i][j][1] = s[i-1][j][1] + s[i][j-1][1] - s[i-1][j-1][1]
if grid[i-1][j-1] == 'Y' {
s[i][j][1]++
}
if s[i][j][0] > 0 && s[i][j][0] == s[i][j][1] {
ans++
}
}
}
return
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 | function numberOfSubmatrices(grid: string[][]): number {
const [m, n] = [grid.length, grid[0].length];
const s = Array.from({ length: m + 1 }, () => Array.from({ length: n + 1 }, () => [0, 0]));
let ans = 0;
for (let i = 1; i <= m; ++i) {
for (let j = 1; j <= n; ++j) {
s[i][j][0] =
s[i - 1][j][0] +
s[i][j - 1][0] -
s[i - 1][j - 1][0] +
(grid[i - 1][j - 1] === 'X' ? 1 : 0);
s[i][j][1] =
s[i - 1][j][1] +
s[i][j - 1][1] -
s[i - 1][j - 1][1] +
(grid[i - 1][j - 1] === 'Y' ? 1 : 0);
if (s[i][j][0] > 0 && s[i][j][0] === s[i][j][1]) {
++ans;
}
}
}
return ans;
}
|