Created
December 25, 2020 23:21
-
-
Save muratatak77/48ee92b3eb6086dc06391162999fc5cd to your computer and use it in GitHub Desktop.
280. Wiggle Sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
we can called zig-zag approach | |
if i index is even should be nums[i] > nums[i+1] | |
if i index is odd should be nums[i] < nums[i+1] | |
we can make an if case together reverse appoarch | |
''' | |
from typing import List | |
class Solution: | |
def wiggleSort(self, nums: List[int]) -> None: | |
""" | |
Do not return anything, modify nums in-place instead. | |
""" | |
for i in range(len(nums)-1): | |
if (i%2 == 0 and nums[i]>nums[i+1]) or (i%2 == 1 and nums[i]< nums[i+1]): | |
nums[i], nums[i+1] = nums[i+1], nums[i] | |
return nums | |
nums = [3,5,2,1,6,4] | |
res = Solution().wiggleSort(nums) | |
print("res :", res) | |
''' | |
Medium | |
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] | |
------------------------------ | |
T(N) = O(N) | |
S(N) = O(1) | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment