Leetcode 324 Solution

This article provides solution to leetcode question 324 (wiggle-sort-ii)

https://leetcode.com/problems/wiggle-sort-ii

Solution

class Solution {
public:
    void wiggleSort(vector<int>& nums) {
        if (nums.size() == 1)
            return;

        vector<int> a(nums);
        std::sort(a.begin(), a.end());

        int i = a.size() - 1;
        int k = 1;

        while (k < a.size())
        {
            nums[k] = a[i];
            i--;
            k += 2;
        }

        k = 0;
        while (k < a.size())
        {
            nums[k] = a[i];
            i--;
            k += 2;
        }
    }
};