LeetCode第 254 场周赛第二题——构造元素不等于两相邻元素平均值的数组
题目描述:
给你一个 下标从 0 开始 的数组 nums ,数组由若干 互不相同的 整数组成。你打算重新排列数组中的元素以满足:重排后,数组中的每个元素都 不等于 其两侧相邻元素的 平均值 。
更公式化的说法是,重新排列的数组应当满足这一属性:对于范围 1 <= i < nums.length - 1 中的每个 i ,(nums[i-1] + nums[i+1]) / 2 不等于 nums[i] 均成立 。
返回满足题意的任一重排结果。
示例 1:
输入:nums = [1,2,3,4,5]
输出:[1,2,4,5,3]
解释:
i=1, nums[i] = 2, 两相邻元素平均值为 (1+4) / 2 = 2.5
i=2, nums[i] = 4, 两相邻元素平均值为 (2+5) / 2 = 3.5
i=3, nums[i] = 5, 两相邻元素平均值为 (4+3) / 2 = 3.5
示例 2:
输入:nums = [6,2,0,9,7]
输出:[9,7,6,2,0]
解释:
i=1, nums[i] = 7, 两相邻元素平均值为 (9+6) / 2 = 7.5
i=2, nums[i] = 6, 两相邻元素平均值为 (7+2) / 2 = 4.5
i=3, nums[i] = 2, 两相邻元素平均值为 (6+0) / 2 = 3
提示:
3 <= nums.length <= 10^5
0 <= nums[i] <= 10^5
解题方法:排序+贪心
解题思路:
本题最简单的解法就是排序之后,先放所有奇数位置,再放所有偶数位置,这样就可以保证所有元素的相邻元素要么都比它大,要么都比它小,从而一定符合要求。
- 时间复杂度O(nlogn)
- 空间复杂度O(1)
参考代码(Java)
1 class Solution { 2 public int[] rearrangeArray(int[] nums) { 3 int n = nums.length; 4 Arrays.sort(nums); 5 for (int i = 1; i < n - 1; i += 2) { 6 int temp = nums[i]; 7 nums[i] = nums[i + 1]; 8 nums[i + 1] = temp; 9 } 10 return nums; 11 12 // 1 2 3 4 5 6 13 // 1 3 2 5 4 6 14 } 15 }
lc原题链接:https://leetcode-cn.com/problems/array-with-elements-not-equal-to-average-of-neighbors/

浙公网安备 33010602011771号