-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
101 lines (90 loc) · 2.81 KB
/
main.cpp
File metadata and controls
101 lines (90 loc) · 2.81 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Source: https://leetcode.com/problems/count-submatrices-with-equal-frequency-of-x-and-y
// Title: Count Submatrices With Equal Frequency of X and Y
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given a 2D character matrix `grid`, where `grid[i][j]` is either `'X'`, `'Y'`, or `'.'`, return the number of <button>submatrices</button> that contain:
//
// - `grid[0][0]`
// - an **equal** frequency of `'X'` and `'Y'`.
// - **at least** one `'X'`.
//
// **Example 1:**
//
// ```
// Input: grid = [["X","Y","."],["Y",".","."]]
// Output: 3
// Explanation:
// **https://assets.leetcode.com/uploads/2024/06/07/examplems.png**
// ```
//
// **Example 2:**
//
// ```
// Input: grid = [["X","X"],["X","Y"]]
// Output: 0
// Explanation:
// No submatrix has an equal frequency of `'X'` and `'Y'`.
// ```
//
// **Example 3:**
//
// ```
// Input: grid = [[".","."],[".","."]]
// Output: 0
// Explanation:
// No submatrix has at least one `'X'`.
// ```
//
// **Constraints:**
//
// - `1 <= grid.length, grid[i].length <= 1000`
// - `grid[i][j]` is either `'X'`, `'Y'`, or `'.'`.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <vector>
using namespace std;
// Prefix Sum
//
// Use prefix sum to compute the number of x and y.
class Solution {
public:
int numberOfSubmatrices(const vector<vector<char>>& grid) {
const int m = grid.size(), n = grid[0].size();
auto xCnt = vector<vector<int>>(m + 1, vector<int>(n + 1, 0));
auto yCnt = vector<vector<int>>(m + 1, vector<int>(n + 1, 0));
int count = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
xCnt[i + 1][j + 1] = (grid[i][j] == 'X') + xCnt[i][j + 1] + xCnt[i + 1][j] - xCnt[i][j];
yCnt[i + 1][j + 1] = (grid[i][j] == 'Y') + yCnt[i][j + 1] + yCnt[i + 1][j] - yCnt[i][j];
count += (xCnt[i + 1][j + 1] > 0 && xCnt[i + 1][j + 1] == yCnt[i + 1][j + 1]);
}
}
return count;
}
};
// Prefix Sum
//
// Use 1D array.
class Solution2 {
public:
int numberOfSubmatrices(const vector<vector<char>>& grid) {
const int m = grid.size(), n = grid[0].size();
auto xCurr = vector<int>(n + 1);
auto xPrev = vector<int>(n + 1);
auto yCurr = vector<int>(n + 1);
auto yPrev = vector<int>(n + 1);
int count = 0;
for (int i = 0; i < m; ++i) {
swap(xCurr, xPrev);
swap(yCurr, yPrev);
for (int j = 0; j < n; ++j) {
xCurr[j + 1] = (grid[i][j] == 'X') + xPrev[j + 1] + xCurr[j] - xPrev[j];
yCurr[j + 1] = (grid[i][j] == 'Y') + yPrev[j + 1] + yCurr[j] - yPrev[j];
count += (xCurr[j + 1] > 0 && xCurr[j + 1] == yCurr[j + 1]);
}
}
return count;
}
};