diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 9a762a1..0dae3bc 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -31,4 +31,5 @@ mod p2859_sum_of_values_at_indices_with_k_set_bits; mod p2846_minimum_edge_weight_equilibrium_queries_in_a_tree; mod p2861_maximum_number_of_alloys; mod p365_water_and_jug_problem; -mod p514_freedom_trail; \ No newline at end of file +mod p514_freedom_trail; +mod p2808_minimum_seconds_to_equalize_a_circular_array; \ No newline at end of file diff --git a/src/problem/p2808_minimum_seconds_to_equalize_a_circular_array.rs b/src/problem/p2808_minimum_seconds_to_equalize_a_circular_array.rs new file mode 100644 index 0000000..c3c9862 --- /dev/null +++ b/src/problem/p2808_minimum_seconds_to_equalize_a_circular_array.rs @@ -0,0 +1,47 @@ +/** + * [2808] Minimum Seconds to Equalize a Circular Array + */ +pub struct Solution {} + + +// submission codes start here + +use std::collections::HashMap; + +impl Solution { + pub fn minimum_seconds(nums: Vec) -> i32 { + let mut map = HashMap::new(); + + for (index, num) in nums.iter().enumerate() { + let entry = map.entry(*num).or_insert(Vec::new()); + entry.push(index); + } + + let mut result = i32::MAX; + + for (_, pos) in map { + let mut max_distance = pos[0] + nums.len() - pos.last().unwrap(); + + for i in 1..pos.len() { + max_distance = max_distance.max(pos[i] - pos[i - 1]); + } + + result = result.min(max_distance as i32 / 2); + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2808() { + assert_eq!(Solution::minimum_seconds(vec![1,2,1,2]), 1); + assert_eq!(Solution::minimum_seconds(vec![2,1,3,3,2]), 2); + } +}