75. Sort Colors π₯
Medium
Problem:
Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]Input: nums = [2,0,1]
Output: [0,1,2]What to learn:
three-way-partition(AΒ : array of values, midΒ : value):
i β 0
j β 0
k β size of A - 1
while j <= k:
if A[j] < mid:
swap A[i] and A[j]
i β i + 1
j β j + 1
else if A[j] > mid:
swap A[j] and A[k]
k β k - 1
else:
j β j + 1Solution:
Last updated