From 5751d9d82edeaac71d1184d61f462c04ee7cb8e0 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 25 Apr 2025 14:06:46 +0800 Subject: [PATCH] 20250217 finished. --- src/problem/mod.rs | 2 + ..._appearing_more_than_25_in_sorted_array.rs | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/problem/p1287_element_appearing_more_than_25_in_sorted_array.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 9a16d98..93599e8 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -490,3 +490,5 @@ mod p1552_magnetic_force_between_two_balls; mod p1706_where_will_the_ball_fall; mod p1299_replace_elements_with_greatest_element_on_right_side; + +mod p1287_element_appearing_more_than_25_in_sorted_array; \ No newline at end of file diff --git a/src/problem/p1287_element_appearing_more_than_25_in_sorted_array.rs b/src/problem/p1287_element_appearing_more_than_25_in_sorted_array.rs new file mode 100644 index 0000000..0287497 --- /dev/null +++ b/src/problem/p1287_element_appearing_more_than_25_in_sorted_array.rs @@ -0,0 +1,47 @@ +use std::thread::current; + +/** + * [1287] Element Appearing More Than 25% In Sorted Array + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn find_special_integer(arr: Vec) -> i32 { + let mut last = arr[0]; + let mut count = 1; + let threshold = arr.len() / 4; + + for &i in arr[1..].iter() { + if last == i { + count += 1; + } else { + last = i; + count = 1; + } + + if count > threshold { + return i; + } + } + + last + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_1287() { + assert_eq!( + 6, + Solution::find_special_integer(vec![1, 2, 2, 6, 6, 6, 6, 7, 10]) + ); + assert_eq!(1, Solution::find_special_integer(vec![1, 1])); + } +}