From d8c4583b1157193a195f646e2b54d7408e34a819 Mon Sep 17 00:00:00 2001 From: li-chx Date: Mon, 28 Jul 2025 11:18:09 +0800 Subject: [PATCH] =?UTF-8?q?2044.=20=E7=BB=9F=E8=AE=A1=E6=8C=89=E4=BD=8D?= =?UTF-8?q?=E6=88=96=E8=83=BD=E5=BE=97=E5=88=B0=E6=9C=80=E5=A4=A7=E5=80=BC?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E9=9B=86=E6=95=B0=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8f88470..679815f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,37 +1,19 @@ struct Solution; impl Solution { - pub fn count_hill_valley(nums: Vec) -> i32 { - let mut down = false; - for i in 1..nums.len() { - if nums[i] != nums[0]{ - down = nums[i] < nums[0]; - break; + pub fn count_max_or_subsets(nums: Vec) -> i32 { + let max_or = nums.iter().fold(0, |acc, &x| acc | x); + let mut dp = vec![0;1<nums[i-1] - { - ans += 1; - down = false; - continue; - } - if !down && nums[i] < nums[i-1] { - ans += 1; - down = true; - continue; - } - } - ans + dp.iter().filter(|x| **x == max_or ).count() as i32 } } fn main() { - let sl = Solution::count_hill_valley(vec![6,6,5,5,4,1]); + let sl = Solution::count_max_or_subsets(vec![3,2,1,5]); println!("{}", sl); }