20250217 finished.
This commit is contained in:
parent
7263a2e7f0
commit
5751d9d82e
|
@ -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;
|
|
@ -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>) -> 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]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user