Skip to content
LC-2161 Medium LeetCode

2161. Partition Array According to Given Pivot

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 90% Topics: Array, Two Pointers, Simulation
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(n)

# two pointers
class Solution(object):
    def pivotArray(self, nums, pivot):
        """
        :type nums: List[int]
        :type pivot: int
        :rtype: List[int]
        """
        result = [pivot]*len(nums)
        left, right = 0, len(nums)-sum(x > pivot for x in nums)
        for x in nums:
            if x < pivot:
                result[left] = x
                left += 1
            elif x > pivot:
                result[right] = x
                right += 1
        return result

Solution from kamyu104/LeetCode-Solutions · MIT