leetcode 324 Wiggle Sort 2

利用中位数的概念,中位数就是将一组数分成2等份(若为奇数,则中位数既不属于左也不属于右,所以是2等份),其一组数中任何一个元素都大于等于另一组数

那么我们是不是只要一左一右配合着插入,就保证了差值+-+-+-的要求?

由于题目输入限制了,必定存在解,所以此处我们不需要担心如果重复中位数太多,出现一左一右相等的情况

算法步骤:

1)找到中位数

  利用lc215 Kth Largest...,将k=(nums.length+1) / 2,可以在o(n)内求出中位数

2)一左一右插入

  0 2 4 .. 插大于中位数的

  1 3 5 .. 插小于中位数的

  考虑到中位数可能有重复的情况

  我们从后往前插入小于中位数的值

  从前往后插入大于中位数的值

  剩下的空位补上中位数

代码中为了省事找中位数的部分用的o(klogn)的方法,详情可以看之前博客

 1 class Solution {
 2     public void wiggleSort(int[] nums) {
 3         int median = findKthLargest(nums, (nums.length+1) / 2);
 4         int left = 1;
 5         int right = nums.length % 2 == 0 ? nums.length-2 : nums.length-1;
 6
 7         int[] tmp = new int[nums.length];
 8         for(int i=0; i<nums.length; i++){
 9             if(nums[i] > median){
10                 tmp[left] = nums[i];
11                 left += 2;
12             }else if(nums[i] < median){
13                 tmp[right] = nums[i];
14                 right -= 2;
15             }
16         }
17
18         while(left < nums.length){
19             tmp[left] = median;
20             left += 2;
21         }
22         while(right >= 0){
23             tmp[right] = median;
24             right -= 2;
25         }
26
27         System.arraycopy(tmp, 0, nums, 0, nums.length);
28     }
29     public int findKthLargest(int[] nums, int k) {
30         if(nums.length == 0)
31             return 0;
32         PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
33         int res = 0;
34         for(int num : nums)
35             pq.offer(num);
36
37         while(k != 0){
38             res = pq.poll();
39             k--;
40         }
41
42         return res;
43     }
44
45 }

原文地址:https://www.cnblogs.com/hwd9654/p/10926213.html

时间: 2024-10-15 08:07:28

leetcode 324 Wiggle Sort 2的相关文章

leetcode [324]Wiggle Sort II

Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... Example 1: Input: nums = [1, 5, 1, 1, 6, 4] Output: One possible answer is [1, 4, 1, 5, 1, 6]. Example 2: Input: nums = [1, 3, 2, 2, 3, 1] Output: One po

LeetCode 280. Wiggle Sort (摆动排序)$

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. 题目标签:Array, Sort 题目给了我们一个nums array, 让我们wiggle sort.

[LeetCode#280] Wiggle Sort

Problem: Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. Analysis: Once you encounter a new problem,

280. Wiggle Sort/324. Wiggle Sort II

280 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... Example: Input: nums = [3,5,2,1,6,4] Output: One possible answer is [3,5,1,6,2,4] 280要求的是可以大于等于,其实简单. 方法一: 1. 先排序,得到序列 a0<=a1<=a2&l

324. Wiggle Sort II

Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... Example: (1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. (2) Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2,

LeetCode 280. Wiggle Sort C#

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. Solution: Loop through, when odd index num should be

leetcode笔记:Wiggle Sort

一. 题目描写叙述 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]- For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. 二. 题目分析 题目给出一个未排序的数组,调整元素的大小使其满足nums[0] <=

【LeetCode】排序 sort(共20题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [56]Merge Intervals [57]Insert Interval [75]Sort Colors [147]Insertion Sort List [148]Sort List [164]Maximum Gap [179]Largest Number [242]Valid Anagram [252]Meeting Rooms (2018年11月22日,为

Wiggle Sort II

Wiggle Sort II Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... 注意事项 You may assume all input has valid answer. 样例 Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. Given nums