Compare commits
3 Commits
6e2610c882
...
aa1f5ac8c1
| Author | SHA1 | Date |
|---|---|---|
|
|
aa1f5ac8c1 | |
|
|
ea455b5577 | |
|
|
625be9225d |
55
src/main.rs
55
src/main.rs
|
|
@ -2,21 +2,38 @@ mod arr;
|
||||||
struct Solution {}
|
struct Solution {}
|
||||||
|
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn triangle_number(nums: Vec<i32>) -> i32 {
|
pub fn dfs(heights: &Vec<Vec<i32>>,arr: &mut Vec<Vec<bool>>,i:usize,j:usize ) {
|
||||||
if nums.len() < 3 {
|
let dirs = vec![(0,1),(0,-1),(1,0),(-1,0)];
|
||||||
return 0;
|
for dir in dirs {
|
||||||
|
let (x,y) = (i as i32 + dir.0, j as i32 + dir.1);
|
||||||
|
if x>=0 && x<heights.len() as i32 && y>=0 && y<heights[0].len() as i32 && !arr[x as usize][y as usize] && heights[x as usize][y as usize]>=heights[i][j] {
|
||||||
|
arr[x as usize][y as usize] = true;
|
||||||
|
Self::dfs(heights,arr,x as usize,y as usize);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let mut nums = nums;
|
}
|
||||||
nums.sort();
|
pub fn pacific_atlantic(heights: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
|
||||||
let mut ans = 0;
|
let (m,n) = (heights.len(),heights[0].len());
|
||||||
for i in 0..nums.len() - 2 {
|
let mut pacific = vec![vec![false;n];m];
|
||||||
let mut r = i + 2;
|
let mut atlantic = vec![vec![false;n];m];
|
||||||
for l in i + 1..nums.len() - 1 {
|
let mut ans = vec![];
|
||||||
while r < nums.len() && nums[i] + nums[l] > nums[r] {
|
for i in 0..m {
|
||||||
r += 1;
|
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 += ((r - l) as i32 - 1).max(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ans
|
ans
|
||||||
|
|
@ -24,6 +41,14 @@ impl Solution {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let arr = vec![7, 0, 0, 0];
|
// [[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]]
|
||||||
println!("{}", Solution::triangle_number(arr));
|
// 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));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue