Problem Description
You are given an array
nums
with \( n \) objects colored red, white, or blue. Sort the array in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue. - Represent the colors using integers:
-
0
β Red -
1
β White -
2
β Blue β οΈ Note: You must not use 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]
---
π Practice Questions for A2SV Preparation
1. Core Problem:
- Solve LeetCode 75: Sort Colors () using Dutch National Flag Algorithm for \( O(n) \) time complexity.
2. Related Problems:
- LeetCode 215 : Kth Largest Element in an Array
- LeetCode 347 : Top K Frequent Elements
- LeetCode 56 : Merge Intervals
3. Challenge Problem:
- LeetCode 88: Merge Sorted Array
---
π‘ Tip: Use a two-pointer or three-pointer approach to keep track of boundaries for the different colors! Try to optimize your solution to \( O(n) \) in both time and space.
π§ Share your solution and discuss your approach with the community! π