From aa1f5ac8c164332ed0e9755107fc71da61643a02 Mon Sep 17 00:00:00 2001 From: li-chx Date: Sun, 5 Oct 2025 02:41:06 +0800 Subject: [PATCH] =?UTF-8?q?417.=20=E5=A4=AA=E5=B9=B3=E6=B4=8B=E5=A4=A7?= =?UTF-8?q?=E8=A5=BF=E6=B4=8B=E6=B0=B4=E6=B5=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 51 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index e78631f..9bba03b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,18 +2,53 @@ mod arr; struct Solution {} impl Solution { - pub fn max_bottles_drunk(mut num_bottles: i32,mut num_exchange: i32) -> i32 { - let mut ans = num_bottles; - while num_bottles >= num_exchange { - num_bottles -= num_exchange; - ans += 1; - num_bottles += 1; - num_exchange += 1; + pub fn dfs(heights: &Vec>,arr: &mut Vec>,i:usize,j:usize ) { + let dirs = vec![(0,1),(0,-1),(1,0),(-1,0)]; + for dir in dirs { + let (x,y) = (i as i32 + dir.0, j as i32 + dir.1); + if x>=0 && x=0 && y=heights[i][j] { + arr[x as usize][y as usize] = true; + Self::dfs(heights,arr,x as usize,y as usize); + } + } + } + pub fn pacific_atlantic(heights: Vec>) -> Vec> { + let (m,n) = (heights.len(),heights[0].len()); + let mut pacific = vec![vec![false;n];m]; + let mut atlantic = vec![vec![false;n];m]; + let mut ans = vec![]; + for i in 0..m { + pacific[i][0] = true; + atlantic[i][n-1] = true; + Self::dfs(&heights,&mut pacific,i,0); + Self::dfs(&heights,&mut atlantic,i,n-1); + } + for j in 0..n { + pacific[0][j] = true; + atlantic[m-1][j] = true; + Self::dfs(&heights,&mut pacific,0,j); + Self::dfs(&heights,&mut atlantic,m-1,j); + } + for i in 0..m { + for j in 0..n { + if pacific[i][j] && atlantic[i][j] { + ans.push(vec![i as i32,j as i32]); + } + } } ans } } fn main() { - println!("{}", Solution::max_bottles_drunk(10,3)); + // [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]] + // initial as a 2D vector + let arr = vec![ + vec![1, 2, 2, 3, 5], + vec![3, 2, 3, 4, 4], + vec![2, 4, 5, 3, 1], + vec![6, 7, 1, 4, 5], + vec![5, 1, 1, 2, 4], + ]; + println!("{:?}", Solution::pacific_atlantic(arr)); }