↓Skip to main content
  1. LeetCode/

LeetCode 75: Sort Colors

·2 mins· ·
LeetCode Medium Array Two-Pointers Sorting Quicksort
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-10-26

  • Submissions: Accepted ×2 → Runtime Error → Wrong Answer ×3 → Runtime Error ×5 → Wrong Answer → Accepted ×5

Approach
#

Three pointers (Two Pointers Template 1): [0, low) holds 0s, (high, n) holds 2s, and i is the current index:

  • nums[i] == 0: swap with low, advance both (what comes back is a 1).
  • nums[i] == 2: swap with high, high -= 1, and keep i (the swapped-in value is unchecked).
  • nums[i] == 1: advance i.

My Attempts
#

My first accepted version counted the 0s, 1s and 2s and refilled the array (two passes). Practicing the one-pass version with for i in range(len(nums)), i still advanced after swapping with high, so the incoming value went unchecked and [2, 1] stayed unchanged; a few versions also misspelled variable names.

Solution
#

class Solution:
    def sortColors(self, nums: List[int]) -> None:
        low, i, high = 0, 0, len(nums) - 1
        while i <= high:
            if nums[i] == 0:
                nums[low], nums[i] = nums[i], nums[low]
                low += 1
                i += 1
            elif nums[i] == 2:
                nums[high], nums[i] = nums[i], nums[high]
                high -= 1
            else:
                i += 1

Complexity
#

Time O(n) in one pass, space O(1).

Mistakes to Avoid
#

  • Do not advance i after swapping with high.
  • Loop while i <= high, not i < n.

Interview Explanation
#

With the Dutch-flag pointers, 0s swap left and 2s swap right; after a right swap the incoming value is unchecked, so the index stays. One pass, O(n).

Related

LeetCode 56: Merge Intervals
·2 mins
LeetCode Medium Array Sorting Quicksort
LeetCode 56 note: sort by start; if a new interval starts at or before the last merged end, merge (taking the max end), otherwise open a new one
LeetCode 15: 3Sum
·2 mins
LeetCode Medium Array Two-Pointers Sorting
LeetCode 15 note: sort, fix the first number, and find the other two with converging pointers, skipping equal values at all three positions to avoid duplicate triplets
LeetCode 16: 3Sum Closest
·2 mins
LeetCode Medium Array Two-Pointers Sorting
LeetCode 16 note: sort, fix one number, and find the other two with converging pointers, comparing the distance abs(sum - target)
LeetCode 18: 4Sum
·2 mins
LeetCode Medium Array Two-Pointers Sorting
LeetCode 18 note: sort, fix the first two numbers with two loops, and converge pointers for the rest, skipping equal values at all four positions
LeetCode 80: Remove Duplicates from Sorted Array II
·1 min
LeetCode Medium Array Two-Pointers
LeetCode 80 note: keep each value at most twice by comparing with nums[k - 2], two slots back from the write position
LeetCode 47: Permutations II
·2 mins
LeetCode Medium Array Backtracking Sorting
LeetCode 47 note: sort first, and within a level skip a value equal to the previous one when the previous one is unused