使循环数组所有元素相等的最少秒数
难度:
标签:
题目描述
You are given a 0-indexed array nums
containing n
integers.
At each second, you perform the following operation on the array:
- For every index
i
in 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 nums
equal.
Example 1:
Input: nums = [1,2,1,2] Output: 1 Explanation: We can equalize the array in 1 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.
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 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.
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:
1 <= n == nums.length <= 105
1 <= nums[i] <= 109
代码结果
运行时间: 119 ms, 内存: 43.4 MB
// Java Stream Solution
// Similar to the previous Java solution but utilizing Java Streams for a more concise approach.
// We count the frequency of each element using a HashMap and find the maximum frequency.
// The result is the difference between the length of the array and the maximum frequency of any element.
import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class StreamSolution {
public int minSecondsToEqual(int[] nums) {
Map<Integer, Long> countMap = Arrays.stream(nums)
.boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
long maxFreq = countMap.values().stream().max(Long::compare).orElse(0L);
return (int) (nums.length - maxFreq);
}
}
解释
方法:
该题解的思路是首先统计数组中每个不同元素的最左出现位置(leftMost)和最右出现位置(rightMost),然后对于每个元素计算其在数组中的所有相邻出现位置之间的距离,并将这些距离存储在一个字典(dist)中。最后,对于每个元素,将其最左出现位置到最右出现位置的距离也加入到dist中。这样,对于每个元素,dist中存储的就是将其转换为相同元素所需的最小步数的候选值。最后,遍历dist,找到最小的最大距离,即为所需的最小秒数。
时间复杂度:
O(n)
空间复杂度:
O(n)
代码细节讲解
🦆
在题解中,为什么要统计每个元素的最左和最右出现位置?这对解题有什么帮助?
▷🦆
题解中提到的距离存储字典(dist)的结构是如何设计的?能否详细解释这个字典中各个键值对的意义?
▷🦆
如何理解题解中提到的将最左出现位置到最右出现位置的距离也加入到dist中这一操作?这样做的目的是什么?
▷🦆
题解中最终返回的结果为什么要进行(ans//2)操作?这一步的数学逻辑和目的是什么?
▷