From 854d77f03f94ed3e56e1c5ea9ab13afd52f5b3f4 Mon Sep 17 00:00:00 2001 From: li-chx Date: Wed, 30 Sep 2026 16:38:03 +0800 Subject: [PATCH] =?UTF-8?q?1111.=20=E6=9C=89=E6=95=88=E6=8B=AC=E5=8F=B7?= =?UTF-8?q?=E7=9A=84=E5=B5=8C=E5=A5=97=E6=B7=B1=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 73 ++++++++++++++++++----------------------------------- 1 file changed, 25 insertions(+), 48 deletions(-) diff --git a/src/main.rs b/src/main.rs index 01ec1bd..d48c68c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,66 +1,43 @@ -use crate::arr::make_char_matrix; -use std::collections::HashSet; mod arr; mod list; struct Solution {} impl Solution { - pub fn has_valid_path(grid: Vec>) -> bool { - if grid[0][0] == ')' - || grid[grid.len() - 1][grid[0].len() - 1] == '(' - || (grid.len() + grid[0].len()) % 2 == 0 - { - return false; + pub fn max_depth_after_split(seq: String) -> Vec { + let seq = seq.as_bytes(); + let mut depth = 0; + let mut max_depth = 0; + for c in seq { + if *c == '(' as u8 { + depth += 1; + max_depth = max_depth.max(depth); + } else { + depth -= 1; + } } - let mut matrix = vec![vec![Option::>::None; grid[0].len()]; grid.len()]; - 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 { - dst.extend( - src.iter() - .filter(|&x| x / 10000 > x % 10000) - .map(|&x| x + 1), - ); - } - } - if grid[0].len() > i - j + 1 { - let (rsrc, rdst) = matrix[j].split_at_mut(i - j + 1); - let dst = rdst[0].get_or_insert_with(HashSet::new); - let src = rsrc[i - j].as_ref().unwrap(); - if grid[j][i - j + 1] == '(' { - dst.extend(src.iter().map(|&x| x + 10000)); - } else { - dst.extend( - src.iter() - .filter(|&x| x / 10000 > x % 10000) - .map(|&x| x + 1), - ); - } - } + let target_depth = max_depth / 2; + let mut ans = vec![0; seq.len()]; + for i in 0..seq.len() { + if seq[i] == '(' as u8 { + depth += 1; + if depth > target_depth { + ans[i] = 1; + } + } else { + depth -= 1; + if depth + 1 > target_depth { + ans[i] = 1; } } } - matrix[grid.len() - 1][grid[0].len() - 1].as_ref() - .unwrap() - .iter() - .any(|&x| x / 10000 == x % 10000) + ans } } fn main() { println!( "{:?}", - Solution::has_valid_path(make_char_matrix( - r#"[["(","(","("],[")","(",")"],["(","(",")"],["(","(",")"]]"# - )) + Solution::max_depth_after_split("()(())()".to_string()) ); }