Partition Array
Description
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.
Notice You should do really partition in array nums instead of just counting the numbers of integers smaller than k. If all elements in nums are smaller than k, then return nums.length
Example If nums = [3,2,2,1] and k=2, a valid answer is 1.
Challenge
Can you partition the array in-place and in O(n)?
We are here
Link
Method
- Similar to the Sort Colors
- x
Example
- 1
class Solution { public: int partitionArray(vector<int> &nums, int k) { // write your code here if (nums.empty()) { return 0; } int n = nums.size(); int start = 0; int end = n-1; int i = 0; while (i <= end) { if (nums[i] < k) { swap(nums[i], nums[start]); start++; i++; } else { swap(nums[i], nums[end]); end--; } } return start; } };
Similar problems
x
Tags
x