Class Solution
-
- All Implemented Interfaces:
public final class Solution2808 - Minimum Seconds to Equalize a Circular Array.
Medium
You are given a 0-indexed array
numscontainingnintegers.At each second, you perform the following operation on the array:
For every index
iin the range[0, n - 1], replacenums[i]with eithernums[i],nums[(i - 1 + n) % n], ornums[(i + 1) % n].
Note that all the elements get replaced simultaneously.
Return the minimum number of seconds needed to make all elements in the array
numsequal.Example 1:
Input: nums = 1,2,1,2
Output: 1
Explanation: We can equalize the array in 1 second in the following way:
At 1<sup>st</sup> second, replace values at each index with [nums3,nums1,nums3,nums3]. After replacement, nums = 2,2,2,2. It can be proven that 1 second is the minimum amount of seconds needed for equalizing the array.
Example 2:
Input: nums = 2,1,3,3,2
Output: 2
Explanation: We can equalize the array in 2 seconds in the following way:
At 1<sup>st</sup> second, replace values at each index with [nums0,nums2,nums2,nums2,nums3]. After replacement, nums = 2,3,3,3,3.
At 2<sup>nd</sup> second, replace values at each index with [nums1,nums1,nums2,nums3,nums4]. After replacement, nums = 3,3,3,3,3.
It can be proven that 2 seconds is the minimum amount of seconds needed for equalizing the array.
Example 3:
Input: nums = 5,5,5,5
Output: 0
Explanation: We don't need to perform any operations as all elements in the initial array are the same.
Constraints:
<code>1 <= n == nums.length <= 10<sup>5</sup></code>
<code>1 <= numsi<= 10<sup>9</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerminimumSeconds(List<Integer> nums)-
-
Method Detail
-
minimumSeconds
final Integer minimumSeconds(List<Integer> nums)
-
-
-
-