Partition Array
Given an array nums of integers and an int k, partition the array (i.e move the elements in "nums") such that:
All elements < k are moved to the left
All elements >= k are moved to the right
Return the partitioning index, i.e the first index i nums[i] >= k.
Example
If nums = [3,2,2,1] and k=2, a valid answer is 1.
Solution
Partition value is the key to quick sort and this question.
This question is classic two pointers question.
the inner while is the slide operation
it's very important left value < k and right value >=k ?
Last updated
Was this helpful?