Sort Colors - Complete Solution Guide
Sort Colors is LeetCode problem 75, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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] Constraints: n == nums.length 1 <= n <=
Detailed Explanation
The "Sort Colors" problem asks us to sort an array of integers in-place, where each integer represents a color: 0 (red), 1 (white), and 2 (blue). The goal is to rearrange the array so that all the 0s come first, followed by all the 1s, and then all the 2s. We are given that the array contains only 0s, 1s, and 2s, and we must do this without using any built-in sorting functions provided by the programming language.
Solution Approach
The provided solution uses the Dutch National Flag algorithm (also known as the three-way partitioning algorithm). This algorithm iterates through the array using a `mid` pointer. It maintains three regions in the array: elements from `0` to `low-1` are all 0s, elements from `low` to `mid-1` are all 1s, and elements from `high+1` to the end of the array are all 2s. The algorithm processes the element at `nums[mid]` based on its value. If it's 0, it swaps it with the element at `nums[low]` and increments both `low` and `mid`. If it's 1, it simply increments `mid`. If it's 2, it swaps it with the element at `nums[high]` and decrements `high`. The algorithm continues until `mid` crosses `high`.
Step-by-Step Algorithm
- Step 1: Initialize three pointers: `low` to 0, `mid` to 0, and `high` to `nums.length - 1`.
- Step 2: While `mid` is less than or equal to `high`, repeat steps 3-5.
- Step 3: If `nums[mid]` is 0, swap `nums[mid]` with `nums[low]`, increment both `low` and `mid`.
- Step 4: If `nums[mid]` is 1, increment `mid`.
- Step 5: If `nums[mid]` is 2, swap `nums[mid]` with `nums[high]`, decrement `high`. Note that `mid` is not incremented in this case because the swapped element from `high` could be any of the three values (0, 1, or 2) and needs to be checked.
- Step 6: The loop terminates when `mid` becomes greater than `high`, at which point the array is sorted.
Key Insights
- Insight 1: The key is to realize that we don't need to use a general-purpose sorting algorithm. Since we know the exact range of values (0, 1, and 2), we can use a more efficient counting or partitioning approach.
- Insight 2: A two-pointer (or three-pointer) approach allows us to sort the array in-place with constant extra space. This eliminates the need for auxiliary arrays.
- Insight 3: Maintaining three pointers (low, mid, high) and using them to partition the array based on the value at the `mid` pointer allows for a single-pass solution.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Two Pointers, Sorting.
Companies
Asked at: Adobe, Amazon, Apple, Arcesium, Autodesk, Bloomberg, DE Shaw, Docusign, Flipkart, Groww, Meta, Microsoft, Morgan Stanley, Optum, Oracle, PayPal, PhonePe, Pocket Gems, Salesforce, Samsung, Sprinklr, Swiggy, Target, TikTok, Uber, Walmart Labs, Yahoo, Zoho, Zopsmart, eBay, tcs.