Partition array according to given pivot
September 10, 2022
array-and-hashmapProblem URL: Partition array according to given pivot
We will take 3 list, one for less, one for equal and one for more than the pivot value. Then we iterate over the input list, append the value to the respective list. Finally we combine 3 list, and return as result.
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
less, equal, more = [], [], []
for num in nums:
if num < pivot:
less.append(num)
elif num > pivot:
more.append(num)
else:
equal.append(num)
return less + equal + more
Time Complexity: O(n)
Space Complexity: O(n)