Medium
Given an array nums 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.
We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.
You must solve this problem without using the library's sort function.
Example 1:
Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Example 2:
Input: nums = [2,0,1]
Output: [0,1,2]
Example 3:
Input: nums = [0]
Output: [0]
Example 4:
Input: nums = [1]
Output: [1]
Constraints:
n == nums.length1 <= n <= 300nums[i]is0,1, or2.
Follow up: Could you come up with a one-pass algorithm using only constant extra space?
To solve this task using Python with a Solution class, you can follow these steps:
- Define a class named
Solution. - Inside the class, define a method named
sortColorsthat takesnumsas an input parameter. - Implement the one-pass algorithm to sort the colors in-place.
- Use three pointers:
left,right, andcurr. - Initialize
leftto 0,righttolen(nums) - 1, andcurrto 0. - Iterate through the array while
curris less than or equal toright. - If
nums[curr]is 0, swapnums[curr]withnums[left], increment bothleftandcurr. - If
nums[curr]is 2, swapnums[curr]withnums[right], decrementright. - If
nums[curr]is 1, simply incrementcurr. - Repeat steps 6-9 until
curris greater thanright.
Here's the implementation:
class Solution:
def sortColors(self, nums):
left, right, curr = 0, len(nums) - 1, 0
while curr <= right:
if nums[curr] == 0:
nums[curr], nums[left] = nums[left], nums[curr]
left += 1
curr += 1
elif nums[curr] == 2:
nums[curr], nums[right] = nums[right], nums[curr]
right -= 1
else:
curr += 1
# Example usage:
solution = Solution()
nums1 = [2,0,2,1,1,0]
solution.sortColors(nums1)
print(nums1) # Output: [0,0,1,1,2,2]
nums2 = [2,0,1]
solution.sortColors(nums2)
print(nums2) # Output: [0,1,2]
nums3 = [0]
solution.sortColors(nums3)
print(nums3) # Output: [0]
nums4 = [1]
solution.sortColors(nums4)
print(nums4) # Output: [1] This solution sorts the colors in-place using a one-pass algorithm with constant extra space. It traverses the array only once, so the time complexity is O(n), where n is the length of the array nums.
from typing import List
class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
zeroes = 0
ones = 0
for i in range(len(nums)):
if nums[i] == 0:
nums[zeroes] = 0
zeroes += 1
elif nums[i] == 1:
ones += 1
for j in range(zeroes, zeroes + ones):
nums[j] = 1
for k in range(zeroes + ones, len(nums)):
nums[k] = 2