Logo
Overview
LeetCode #75 - Sort Colors

LeetCode #75 - Sort Colors

Jane Doe Jane Doe
August 1, 2025 3 min read
index

πŸ“ Introduction

Given an array nums with n objects colored red (0), white (1), or blue (2), sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

*Constraints typically include:

  • You must solve this problem without using the library’s sort function.
  • The array contains only values 0, 1, and 2.*

πŸ’‘ Approach & Key Insights

The problem can be solved through various approaches. Though sorting is a valid but not expected solution, we can explore better and optimal methods by leveraging the limited number of distinct values (0, 1, 2). This allows for more efficient counting or pointer-based techniques like the Dutch National Flag algorithm.


πŸ› οΈ Breakdown of Approaches

1️⃣ Brute Force / Naive Approach

  • Explanation: Use the built-in sorting algorithm to sort the array. This works since the array contains only 0, 1, and 2, and sorting would naturally arrange them.
  • Time Complexity:Β O(n log n) – standard sorting complexity.
  • Space Complexity:Β O(1) – assuming in-place sort.
  • Example/Dry Run:
Input: [2, 0, 2, 1, 1, 0]
Sorted Output: [0, 0, 1, 1, 2, 2]

2️⃣ Better Approach

  • Explanation: Count the number of 0s, 1s, and 2s in one pass. Then overwrite the array in a second pass by placing the right number of 0s, 1s, and 2s.
  • Time Complexity:Β O(n) – 2 passes through the array.
  • Space Complexity:Β O(1) – constant space for counters.
  • Example/Dry Run:
Input: [2, 0, 2, 1, 1, 0]
Step 1: Count β†’ count_0 = 2, count_1 = 2, count_2 = 2
Step 2: Overwrite:
[0, 0, 1, 1, 2, 2]

3️⃣ Optimal Approach

  • Explanation:Β Use the Dutch National Flag algorithm with three pointers (low, mid, high). Rearrange elements in a single pass.
  • Time Complexity:Β O(n) – single pass.
  • Space Complexity:Β O(1) – in-place.
  • Example/Dry Run:
Input: [2, 0, 2, 1, 1, 0]
Initial: low = 0, mid = 0, high = 5
Step-by-step:
mid = 0, nums[mid] = 2 β†’ swap(nums[mid], nums[high]) β†’ high--
mid = 0, nums[mid] = 0 β†’ swap(nums[low], nums[mid]) β†’ low++, mid++
mid = 1, nums[mid] = 0 β†’ swap(nums[low], nums[mid]) β†’ low++, mid++
mid = 2, nums[mid] = 2 β†’ swap(nums[mid], nums[high]) β†’ high--
mid = 2, nums[mid] = 1 β†’ mid++
mid = 3, nums[mid] = 1 β†’ mid++
Final Output: [0, 0, 1, 1, 2, 2]

πŸ“Š Complexity Analysis

ApproachTime ComplexitySpace Complexity
Brute ForceO(n log n)O(1)
CountingO(n)O(1)
Dutch Flag (Optimal)O(n)O(1)

πŸ“‰ Optimization Ideas

The Dutch National Flag algorithm is already optimal β€” it solves the problem in a single traversal with constant space. No further optimization is necessary.


πŸ“Œ Example Walkthroughs & Dry Runs

Example:
Input: [2, 0, 1]
Initial: low = 0, mid = 0, high = 2
1. nums[mid] = 2 β†’ swap(nums[mid], nums[high]) β†’ [1, 0, 2], high--
2. nums[mid] = 1 β†’ mid++
3. nums[mid] = 0 β†’ swap(nums[low], nums[mid]) β†’ [0, 1, 2], low++, mid++
Output: [0, 1, 2]

πŸ”— Additional Resources


Author: Neha Amin Date: 19/07/2025

Related Posts