Three-Way Partitioning (Dutch National Flag)

easy
Unit 3 · Arrays & Two Pointers0/3 to milestone

Dijkstra's Dutch national flag problem: rearrange a list in place, in one pass, so that every value below a pivot comes first, then values equal to it, then values above it.

Implement:

  • partition3(nums, pivot): rearrange nums in place using three pointers (low, mid, high) and return (lt, gt), where nums[:lt] are less than the pivot, nums[lt:gt] are equal and nums[gt:] are greater
  • sort_colors(nums): sort a list containing only 0, 1 and 2 in place with one call to partition3
#two-pointers#partitioning#sorting2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Three-Way Partitioning (Dutch National Flag) · TechGradify