1111. 有效括号的嵌套深度
This commit is contained in:
+22
-45
@@ -1,66 +1,43 @@
|
|||||||
use crate::arr::make_char_matrix;
|
|
||||||
use std::collections::HashSet;
|
|
||||||
|
|
||||||
mod arr;
|
mod arr;
|
||||||
mod list;
|
mod list;
|
||||||
|
|
||||||
struct Solution {}
|
struct Solution {}
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn has_valid_path(grid: Vec<Vec<char>>) -> bool {
|
pub fn max_depth_after_split(seq: String) -> Vec<i32> {
|
||||||
if grid[0][0] == ')'
|
let seq = seq.as_bytes();
|
||||||
|| grid[grid.len() - 1][grid[0].len() - 1] == '('
|
let mut depth = 0;
|
||||||
|| (grid.len() + grid[0].len()) % 2 == 0
|
let mut max_depth = 0;
|
||||||
{
|
for c in seq {
|
||||||
return false;
|
if *c == '(' as u8 {
|
||||||
}
|
depth += 1;
|
||||||
let mut matrix = vec![vec![Option::<HashSet<i32>>::None; grid[0].len()]; grid.len()];
|
max_depth = max_depth.max(depth);
|
||||||
matrix[0][0] = Some(HashSet::from_iter([10000]));
|
|
||||||
for i in 0..=(grid.len() - 1 + grid[0].len() - 1) {
|
|
||||||
for j in 0..=i {
|
|
||||||
if grid.len() > j && grid[0].len() > i - j {
|
|
||||||
if grid.len() > j + 1 {
|
|
||||||
let (rsrc, rdst) = matrix.split_at_mut(j + 1);
|
|
||||||
let dst = rdst[0][i - j].get_or_insert_with(HashSet::new);
|
|
||||||
let src = rsrc[j][i - j].as_ref().unwrap();
|
|
||||||
if grid[j + 1][i - j] == '(' {
|
|
||||||
dst.extend(src.iter().map(|&x| x + 10000));
|
|
||||||
} else {
|
} else {
|
||||||
dst.extend(
|
depth -= 1;
|
||||||
src.iter()
|
|
||||||
.filter(|&x| x / 10000 > x % 10000)
|
|
||||||
.map(|&x| x + 1),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if grid[0].len() > i - j + 1 {
|
let target_depth = max_depth / 2;
|
||||||
let (rsrc, rdst) = matrix[j].split_at_mut(i - j + 1);
|
let mut ans = vec![0; seq.len()];
|
||||||
let dst = rdst[0].get_or_insert_with(HashSet::new);
|
for i in 0..seq.len() {
|
||||||
let src = rsrc[i - j].as_ref().unwrap();
|
if seq[i] == '(' as u8 {
|
||||||
if grid[j][i - j + 1] == '(' {
|
depth += 1;
|
||||||
dst.extend(src.iter().map(|&x| x + 10000));
|
if depth > target_depth {
|
||||||
|
ans[i] = 1;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
dst.extend(
|
depth -= 1;
|
||||||
src.iter()
|
if depth + 1 > target_depth {
|
||||||
.filter(|&x| x / 10000 > x % 10000)
|
ans[i] = 1;
|
||||||
.map(|&x| x + 1),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
ans
|
||||||
}
|
|
||||||
matrix[grid.len() - 1][grid[0].len() - 1].as_ref()
|
|
||||||
.unwrap()
|
|
||||||
.iter()
|
|
||||||
.any(|&x| x / 10000 == x % 10000)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!(
|
println!(
|
||||||
"{:?}",
|
"{:?}",
|
||||||
Solution::has_valid_path(make_char_matrix(
|
Solution::max_depth_after_split("()(())()".to_string())
|
||||||
r#"[["(","(","("],[")","(",")"],["(","(",")"],["(","(",")"]]"#
|
|
||||||
))
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user