Skip to the code
cf.
+25 XP

#197 Partition Around the Last Card

medium

A kanban board sorts cards by estimate with quick sort, and you are writing its partition step. It works on nums in place, exactly like this:

  • The pivot is the last item, pivot = nums[n - 1], and store starts at 0.
  • For j from 0 to n - 2: if nums[j] < pivot, swap nums[store] and nums[j], then add 1 to store.
  • Finally swap nums[store] and nums[n - 1], which puts the pivot at index store.

Write partitionLast(nums) that changes nums in place as described. The tests check nums afterwards. An empty list stays empty.

Example 1

Input: nums = [7,2,9,4,3,8,5]
Output: [2,4,3,5,9,8,7]

2, 4, and 3 are smaller than the pivot 5 and move to the front in that order; 5 lands at index 3.

Example 2

Input: nums = [1,2,3]
Output: [1,2,3]

Both items are smaller than 3, and each swaps with itself.

Constraints

  • 0 <= nums.length <= 1000
  • -10^6 <= nums[i] <= 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).