Wiggle Sort 系列題目

LeetCode 280 Wiggle Sort

LeetCode 280
題目數組

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]....app

Example:ui

Input: nums = [3,5,2,1,6,4]
Output: One possible answer is [3,5,1,6,2,4]this

根據題意咱們能夠得知,當i爲奇數時,nums[i - 1] <= nums[i], 當i爲偶數時,num[i - 1] >= nums[i], 遍歷數組,遇到奇數且不知足條件的,兩兩交換;遇到偶數且不知足條件的,兩兩交換。由於有這個=號,因此比wiggle sort ii要簡單不少。spa

public void wiggleSort(int[] nums) {
        for(int i = 1; i < nums.length; i++) {
            if (i % 2 == 1 && nums[i] < nums[i - 1]) {
                swap(i, i - 1, nums);
            } else if (i% 2 == 0 && nums[i] > nums[i - 1]) {
                swap(i, i - 1, nums);
            }
        }
    }
    private void swap(int i, int j, int[] nums) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

LeetCode 215 Find Kth Largest Element in Array

LeetCode215
題目
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.指針

Example 1:code

Input: [3,2,1,5,6,4] and k = 2
Output: 5
Example 2:遞歸

Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.ip

解題思路:QuickSelectelement

public int findKthLargest(int[] nums, int k) {
        //quickselect
        if (nums == null || nums.length == 0) {
            return -1;
        }
        return quickSelect(nums, 0, nums.length - 1, k);  
    }
    private int quickSelect(int[] nums, int start, int end, int k) {
        //遞歸的出口
        if (start == end) {
            return nums[start];
        }
        int i = start;
        int j = end;
        int pivot = nums[(i + j) /2];
        //partition
        while (i <= j) {
            while (i <= j && nums[i] > pivot) {
                i++;
            }
            while (i <= j && nums[j] < pivot) {
                j--;
            }
            if ( i <= j ) {
                //swap
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
                i++;
                j--;
            }
        }
        //target is on the left
        if (start + k - 1 <= j) {
            return quickSelect(nums, start, j, k);
        }
        //target is on the right
        if (start + k - 1 >= i) {
            return quickSelect(nums, i, end, k - (i - start));
        }
        return nums[j + 1];
        
    }

LeetCode 75 Sort Colors

LeetCode75

題目:
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Note: You are not suppose to use the library's sort function for this problem.

Example:

Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Follow up:

A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
Could you come up with a one-pass algorithm using only constant space?

public void sortColors(int[] nums) {
        //[1,2,0]
        //r    b
        //[1,0,2]
        // r b
        int red = 0, blue = nums.length - 1;
        for(int i = 0; i <= blue; i++) {
            if (nums[i] == 0) {
                swap(red, i, nums);
                red++;
            } else if (nums[i] == 2) {
                swap(blue, i, nums);
                blue--;
                //雖然交換,但i的位置仍是有多是0或者2
                i--;
            }
        }
    }
    public void swap(int i, int j, int[] nums) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

題目中還要讓只遍歷一次數組來求解,那麼我須要用雙指針來作,分別從原數組的首尾往中心移動。

  • 定義red指針指向開頭位置,blue指針指向末尾位置
  • 從頭開始遍歷原數組,若是遇到0,則交換該值和red指針指向的值,並將red指針後移一位。若遇到2,則交換該值和blue指針指向的值,並將blue指針前移一位。若遇到1,則繼續遍歷。

LeetCode 324 Wiggle Sort II

LeetCode324

題目:
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 possible answer is [2, 3, 1, 3, 1, 2].
Note:
You may assume all input has valid answer.

Follow Up:
Can you do it in O(n) time and/or in-place with O(1) extra space?

Wiggle Sort II 是一道與findKthLargestElement結合的題目。

重點:
先用findKthLargestElement的思路將數組進行partition,讓median的左邊全都大於median,右邊全都小於median。舉例來講,[1,4,5,6,1,1] => [5,6,4,1,1,1]

利用virtual indexing的性質,條件爲
mapIndex = (1 + 2 * index) % (n | 1)
n 表明數組個數

能夠將index這樣轉換 -
index 0 1 2 3 4 5
mapp 1 3 5 0 2 4

解題思路:
用quickSelect方法找到median
利用sort color的方法,對mapped index所對應的值進行交換。

public void wiggleSort(int[] nums) {
        int median = findKthLargestElement(nums, nums.length / 2);
        int red = 0;
        int blue = nums.length - 1;
        int n = nums.length;
        int i =0;
        while(i <= blue) {
            if (nums[mapIndex(i, n)] > median) {
                swap(nums, mapIndex(red, n), mapIndex(i, n));
                red++;
                i++;
            } else if (nums[mapIndex(i, n)] < median) {
                swap(nums, mapIndex(blue, n), mapIndex(i, n));
                blue--;
            } else {
                i++;
            }
        }
        return; 
    }
    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
    private int mapIndex(int index, int n) {
        return (1 + 2 * index) % (n | 1);
    }
相關文章
相關標籤/搜索