3212. 统计 X 和 Y 频数相等的子矩阵数量
This commit is contained in:
parent
bb640829ee
commit
bb6a7dc773
123
src/arr.rs
123
src/arr.rs
|
|
@ -79,3 +79,126 @@ pub fn make_matrix(s: &str) -> Vec<Vec<i32>> {
|
||||||
|
|
||||||
rows
|
rows
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn make_char_matrix(s: &str) -> Vec<Vec<char>> {
|
||||||
|
let s = s.trim();
|
||||||
|
|
||||||
|
// Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]"
|
||||||
|
let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') {
|
||||||
|
&s[1..s.len() - 1]
|
||||||
|
} else {
|
||||||
|
s
|
||||||
|
}
|
||||||
|
.trim();
|
||||||
|
|
||||||
|
if inner.is_empty() {
|
||||||
|
return Vec::new();
|
||||||
|
}
|
||||||
|
|
||||||
|
let bytes = inner.as_bytes();
|
||||||
|
let mut i = 0usize;
|
||||||
|
let mut rows: Vec<Vec<char>> = Vec::new();
|
||||||
|
while i < bytes.len() {
|
||||||
|
// find next '['
|
||||||
|
while i < bytes.len() && bytes[i] != b'[' {
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
if i >= bytes.len() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let start = i + 1; // byte index after '['
|
||||||
|
i = start;
|
||||||
|
// find matching ']'
|
||||||
|
while i < bytes.len() && bytes[i] != b']' {
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
let end = i; // exclusive
|
||||||
|
// move i past ']' for next iteration (if in bounds)
|
||||||
|
if i < bytes.len() {
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if end <= start {
|
||||||
|
// empty row like []
|
||||||
|
rows.push(Vec::new());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let row_str = &inner[start..end];
|
||||||
|
let row_vec: Vec<char> = row_str
|
||||||
|
.split(',')
|
||||||
|
.map(|x| {
|
||||||
|
let x = x.trim().trim_matches('"');
|
||||||
|
if x == "null" || x.is_empty() {
|
||||||
|
' '
|
||||||
|
} else {
|
||||||
|
x.to_string().chars().nth(0).unwrap()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
rows.push(row_vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
rows
|
||||||
|
}
|
||||||
|
pub fn make_string_matrix(s: &str) -> Vec<Vec<String>> {
|
||||||
|
let s = s.trim();
|
||||||
|
|
||||||
|
// Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]"
|
||||||
|
let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') {
|
||||||
|
&s[1..s.len() - 1]
|
||||||
|
} else {
|
||||||
|
s
|
||||||
|
}
|
||||||
|
.trim();
|
||||||
|
|
||||||
|
if inner.is_empty() {
|
||||||
|
return Vec::new();
|
||||||
|
}
|
||||||
|
|
||||||
|
let bytes = inner.as_bytes();
|
||||||
|
let mut i = 0usize;
|
||||||
|
let mut rows: Vec<Vec<String>> = Vec::new();
|
||||||
|
while i < bytes.len() {
|
||||||
|
// find next '['
|
||||||
|
while i < bytes.len() && bytes[i] != b'[' {
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
if i >= bytes.len() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let start = i + 1; // byte index after '['
|
||||||
|
i = start;
|
||||||
|
// find matching ']'
|
||||||
|
while i < bytes.len() && bytes[i] != b']' {
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
let end = i; // exclusive
|
||||||
|
// move i past ']' for next iteration (if in bounds)
|
||||||
|
if i < bytes.len() {
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if end <= start {
|
||||||
|
// empty row like []
|
||||||
|
rows.push(Vec::new());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let row_str = &inner[start..end];
|
||||||
|
let row_vec: Vec<String> = row_str
|
||||||
|
.split(',')
|
||||||
|
.map(|x| {
|
||||||
|
let x = x.trim().trim_matches('"');
|
||||||
|
if x == "null" || x.is_empty() {
|
||||||
|
"".to_string()
|
||||||
|
} else {
|
||||||
|
x.to_string()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
rows.push(row_vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
rows
|
||||||
|
}
|
||||||
57
src/main.rs
57
src/main.rs
|
|
@ -1,29 +1,53 @@
|
||||||
struct Solution;
|
struct Solution;
|
||||||
mod arr;
|
mod arr;
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn count_submatrices(mut grid: Vec<Vec<i32>>, k: i32) -> i32 {
|
pub fn number_of_submatrices(mut grid: Vec<Vec<char>>) -> i32 {
|
||||||
|
let mut arr = vec![vec![(0, 0); grid[0].len()]; grid.len()];
|
||||||
|
match grid[0][0] {
|
||||||
|
'.' => arr[0][0] = (0, 0),
|
||||||
|
'X' => arr[0][0] = (0, 1),
|
||||||
|
'Y' => arr[0][0] = (1, 0),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
let mut ans = 0;
|
let mut ans = 0;
|
||||||
if grid[0][0] <= k {
|
|
||||||
ans += 1;
|
|
||||||
}else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
for i in 1..grid[0].len() {
|
for i in 1..grid[0].len() {
|
||||||
grid[0][i] += grid[0][i-1];
|
let (mut y, mut x) = arr[0][i - 1];
|
||||||
if grid[0][i] <= k {
|
match grid[0][i] {
|
||||||
|
'X' => x += 1,
|
||||||
|
'Y' => y += 1,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
if x == y && x != 0 {
|
||||||
ans += 1;
|
ans += 1;
|
||||||
}
|
}
|
||||||
|
arr[0][i] = (y, x)
|
||||||
}
|
}
|
||||||
for i in 1.. grid.len() {
|
|
||||||
grid[i][0] += grid[i-1][0];
|
for i in 1..grid.len() {
|
||||||
if grid[i][0] <= k {
|
let (mut y, mut x) = arr[i - 1][0];
|
||||||
|
match grid[i][0] {
|
||||||
|
'X' => x += 1,
|
||||||
|
'Y' => y += 1,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
arr[i][0] = (y, x);
|
||||||
|
if x == y && x != 0 {
|
||||||
ans += 1;
|
ans += 1;
|
||||||
}
|
}
|
||||||
for j in 1.. grid[i].len() {
|
for j in 1..grid[i].len() {
|
||||||
grid[i][j] += grid[i-1][j] + grid[i][j-1] - grid[i-1][j-1];
|
let (ya, xa) = arr[i - 1][j];
|
||||||
if grid[i][j] <= k {
|
let (yb, xb) = arr[i][j - 1];
|
||||||
|
let (yc, xc) = arr[i - 1][j - 1];
|
||||||
|
let (mut x, mut y) = (xa + xb - xc, ya + yb - yc);
|
||||||
|
match grid[i][j] {
|
||||||
|
'X' => x += 1,
|
||||||
|
'Y' => y += 1,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
if x == y && x != 0 {
|
||||||
ans += 1;
|
ans += 1;
|
||||||
}
|
}
|
||||||
|
arr[i][j] = (y, x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ans
|
ans
|
||||||
|
|
@ -31,9 +55,6 @@ impl Solution {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let result = Solution::count_submatrices(arr::make_matrix(
|
let result = Solution::number_of_submatrices(arr::make_char_matrix(r#"[["X","Y"],["X","Y"]]"#));
|
||||||
"[[7,2,9],[1,5,0],[2,6,6]]"
|
|
||||||
), 20);
|
|
||||||
|
|
||||||
println!("{:?}", result);
|
println!("{:?}", result);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue