Description

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 (0), white (1), and blue (2). You must solve this without using the library's sort function. Return the result as an array.

Examples

Input:nums = [2,0,2,1,1,0]
Output:[0,0,1,1,2,2]
Explanation:

The array contains two 0s (red), two 1s (white), and two 2s (blue). After sorting in-place, all 0s come first, followed by all 1s, then all 2s.

Input:nums = [2,0,1]
Output:[0,1,2]
Explanation:

The array contains one of each color: red (0), white (1), and blue (2). After sorting, they appear in the required order: 0, 1, 2.

Input:nums = [1,1,1,1,1]
Output:[1,1,1,1,1]
Explanation:

All objects are white (1), so the array is already sorted and remains unchanged.

Constraints

  • n == nums.length
  • 1 ≤ n ≤ 300
  • nums[i] is either 0, 1, or 2.

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!