Input: nums =[1,2,1,2] Output:1 Explanation: We can equalize the array in1 second in the following way:- At 1st second, replace values at each index with[nums[3],nums[1],nums[3],nums[3]]. 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.
Input: nums =[2,1,3,3,2] Output:2 Explanation: We can equalize the array in2 seconds in the following way:- At 1st 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].- At 2nd 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]. It can be proven that 2 seconds is the minimum amount of seconds needed for equalizing the array.
For each value, the slowest position to be equalized is the largest gap between its occurrences (circularly). The minimum seconds is the minimum over all values of the maximum gap divided by 2 (rounded up).
#include<vector>#include<unordered_map>#include<algorithm>classSolution {
public:int minimumSeconds(vector<int>& nums) {
int n = nums.size(), res = n;
unordered_map<int,vector<int>> pos;
for (int i =0; i < n; ++i) pos[nums[i]].push_back(i);
for (auto& [v, idxs] : pos) {
int max_gap =0;
for (int i =0; i < idxs.size(); ++i) {
int j = (i+1)%idxs.size();
int gap = (idxs[j]-idxs[i]+n)%n;
max_gap = max(max_gap, gap);
}
res = min(res, max_gap/2);
}
return res;
}
};
import java.util.*;
classSolution {
publicintminimumSeconds(List<Integer> nums) {
int n = nums.size(), res = n;
Map<Integer,List<Integer>> pos =new HashMap<>();
for (int i = 0; i < n; i++) pos.computeIfAbsent(nums.get(i), k->new ArrayList<>()).add(i);
for (List<Integer> idxs : pos.values()) {
int maxGap = 0;
for (int i = 0; i < idxs.size(); i++) {
int j = (i+1)%idxs.size();
int gap = (idxs.get(j)-idxs.get(i)+n)%n;
maxGap = Math.max(maxGap, gap);
}
res = Math.min(res, maxGap/2);
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funminimumSeconds(nums: List<Int>): Int {
val n = nums.size
val pos = mutableMapOf<Int,MutableList<Int>>()
nums.forEachIndexed { i,v -> pos.getOrPut(v){mutableListOf()}.add(i) }
var res = n
for (idxs in pos.values) {
var maxGap = 0for (i in idxs.indices) {
val j = (i+1)%idxs.size
val gap = (idxs[j]-idxs[i]+n)%n
maxGap = maxOf(maxGap, gap)
}
res = minOf(res, maxGap/2)
}
return res
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from collections import defaultdict
classSolution:
defminimumSeconds(self, nums: list[int]) -> int:
n = len(nums)
pos = defaultdict(list)
for i, v in enumerate(nums):
pos[v].append(i)
res = n
for idxs in pos.values():
max_gap =0for i in range(len(idxs)):
j = (i+1)%len(idxs)
gap = (idxs[j]-idxs[i]+n)%n
max_gap = max(max_gap, gap)
res = min(res, max_gap//2)
return res
use std::collections::HashMap;
impl Solution {
pubfnminimum_seconds(nums: Vec<i32>) -> i32 {
let n = nums.len();
letmut pos: HashMap<i32, Vec<usize>>= HashMap::new();
for (i, &v) in nums.iter().enumerate() {
pos.entry(v).or_default().push(i);
}
letmut res = n;
for idxs in pos.values() {
letmut max_gap =0;
for i in0..idxs.len() {
let j = (i+1)%idxs.len();
let gap = (idxs[j]+n-idxs[i])%n;
max_gap = max_gap.max(gap);
}
res = res.min(max_gap/2);
}
res asi32 }
}