Solution: Sort Colors

Let's solve the Sort Colors problem using the Two Pointers pattern.

Statement

Given an array, colors, which contains a combination of the following three elements:

  • 00 (representing red)

  • 11 (representing white)

  • 22 (representing blue)

Sort the array in place so that the elements of the same color are adjacent, with the colors in the order of red, white, and blue. To improve your problem-solving skills, do not utilize the built-in sort function.

Constraints:

  • 11 \leq colors.length \leq 300
  • colors[i] can only contain 00
...
Access this course and 1400+ top-rated courses and projects.