2808. Minimum Seconds to Equalize a Circular Array Medium
1/**
2 * [2808] Minimum Seconds to Equalize a Circular Array
3 *
4 * You are given a 0-indexed array nums containing n integers.
5 * At each second, you perform the following operation on the array:
6 *
7 * For every index i in the range [0, n - 1], replace nums[i] with either nums[i], nums[(i - 1 + n) % n], or nums[(i + 1) % n].
8 *
9 * Note that all the elements get replaced simultaneously.
10 * Return the minimum number of seconds needed to make all elements in the array nums equal.
11 *
12 * <strong class="example">Example 1:
13 *
14 * Input: nums = [1,2,1,2]
15 * Output: 1
16 * Explanation: We can equalize the array in 1 second in the following way:
17 * - At 1^st second, replace values at each index with [nums[3],nums[1],nums[3],nums[3]]. After replacement, nums = [2,2,2,2].
18 * It can be proven that 1 second is the minimum amount of seconds needed for equalizing the array.
19 *
20 * <strong class="example">Example 2:
21 *
22 * Input: nums = [2,1,3,3,2]
23 * Output: 2
24 * Explanation: We can equalize the array in 2 seconds in the following way:
25 * - At 1^st second, replace values at each index with [nums[0],nums[2],nums[2],nums[2],nums[3]]. After replacement, nums = [2,3,3,3,3].
26 * - At 2^nd second, replace values at each index with [nums[1],nums[1],nums[2],nums[3],nums[4]]. After replacement, nums = [3,3,3,3,3].
27 * It can be proven that 2 seconds is the minimum amount of seconds needed for equalizing the array.
28 *
29 * <strong class="example">Example 3:
30 *
31 * Input: nums = [5,5,5,5]
32 * Output: 0
33 * Explanation: We don't need to perform any operations as all elements in the initial array are the same.
34 *
35 *
36 * Constraints:
37 *
38 * 1 <= n == nums.length <= 10^5
39 * 1 <= nums[i] <= 10^9
40 *
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/minimum-seconds-to-equalize-a-circular-array/
45// discuss: https://leetcode.com/problems/minimum-seconds-to-equalize-a-circular-array/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50 pub fn minimum_seconds(nums: Vec<i32>) -> i32 {
51 0
52 }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn test_2808() {
63 }
64}
65
Back
© 2025 bowen.ge All Rights Reserved.