LeetCode 3194: Minimum Average of Smallest and Largest Elements (Sort + Pairing)
LeetCode 3194ArraySortingTwo PointersToday we solve LeetCode 3194 - Minimum Average of Smallest and Largest Elements.
Source: https://leetcode.com/problems/minimum-average-of-smallest-and-largest-elements/
English
Problem Summary
Given an even-length integer array, repeatedly pair the current smallest element with the current largest element. For each pair, compute the average. Return the minimum average among all these pairs.
Key Insight
After sorting, the required pairing is fixed: left endpoint with right endpoint, then move inward. So we only need to evaluate (nums[i] + nums[j]) / 2.0 while i < j, and track the minimum.
Algorithm
- Sort nums in non-decreasing order.
- Set two pointers: i = 0, j = n - 1.
- While i < j: compute current pair average, update answer, then i++, j--.
- Return the minimum average found.
Complexity Analysis
Time: O(n log n) (sorting dominates).
Space: O(1) extra (excluding sort implementation details).
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public double minimumAverage(int[] nums) {
Arrays.sort(nums);
int i = 0, j = nums.length - 1;
double ans = Double.POSITIVE_INFINITY;
while (i < j) {
double avg = (nums[i] + nums[j]) / 2.0;
ans = Math.min(ans, avg);
i++;
j--;
}
return ans;
}
}func minimumAverage(nums []int) float64 {
sort.Ints(nums)
i, j := 0, len(nums)-1
ans := math.Inf(1)
for i < j {
avg := float64(nums[i]+nums[j]) / 2.0
if avg < ans {
ans = avg
}
i++
j--
}
return ans
}class Solution {
public:
double minimumAverage(vector<int>& nums) {
sort(nums.begin(), nums.end());
int i = 0, j = (int)nums.size() - 1;
double ans = 1e100;
while (i < j) {
double avg = (nums[i] + nums[j]) / 2.0;
ans = min(ans, avg);
i++;
j--;
}
return ans;
}
};class Solution:
def minimumAverage(self, nums: List[int]) -> float:
nums.sort()
i, j = 0, len(nums) - 1
ans = float('inf')
while i < j:
avg = (nums[i] + nums[j]) / 2.0
ans = min(ans, avg)
i += 1
j -= 1
return ansvar minimumAverage = function(nums) {
nums.sort((a, b) => a - b);
let i = 0, j = nums.length - 1;
let ans = Infinity;
while (i < j) {
const avg = (nums[i] + nums[j]) / 2.0;
ans = Math.min(ans, avg);
i++;
j--;
}
return ans;
};中文
题目概述
给定一个长度为偶数的整数数组。每一步都取当前最小值和当前最大值配对,计算该对的平均值。返回所有配对平均值中的最小值。
核心思路
数组排序后,题目要求的配对方式就唯一确定:最左和最右配对,然后双指针向中间收缩。因此只需在 i < j 时计算 (nums[i] + nums[j]) / 2.0 并维护最小值。
算法步骤
- 将 nums 升序排序。
- 使用双指针 i = 0、j = n - 1。
- 循环中计算当前一对的平均值并更新答案,然后 i++、j--。
- 最终返回最小平均值。
复杂度分析
时间复杂度:O(n log n)(主要来自排序)。
空间复杂度:O(1) 额外空间(不计排序内部开销)。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public double minimumAverage(int[] nums) {
Arrays.sort(nums);
int i = 0, j = nums.length - 1;
double ans = Double.POSITIVE_INFINITY;
while (i < j) {
double avg = (nums[i] + nums[j]) / 2.0;
ans = Math.min(ans, avg);
i++;
j--;
}
return ans;
}
}func minimumAverage(nums []int) float64 {
sort.Ints(nums)
i, j := 0, len(nums)-1
ans := math.Inf(1)
for i < j {
avg := float64(nums[i]+nums[j]) / 2.0
if avg < ans {
ans = avg
}
i++
j--
}
return ans
}class Solution {
public:
double minimumAverage(vector<int>& nums) {
sort(nums.begin(), nums.end());
int i = 0, j = (int)nums.size() - 1;
double ans = 1e100;
while (i < j) {
double avg = (nums[i] + nums[j]) / 2.0;
ans = min(ans, avg);
i++;
j--;
}
return ans;
}
};class Solution:
def minimumAverage(self, nums: List[int]) -> float:
nums.sort()
i, j = 0, len(nums) - 1
ans = float('inf')
while i < j:
avg = (nums[i] + nums[j]) / 2.0
ans = min(ans, avg)
i += 1
j -= 1
return ansvar minimumAverage = function(nums) {
nums.sort((a, b) => a - b);
let i = 0, j = nums.length - 1;
let ans = Infinity;
while (i < j) {
const avg = (nums[i] + nums[j]) / 2.0;
ans = Math.min(ans, avg);
i++;
j--;
}
return ans;
};
Comments