|
| 1 | +/** |
| 2 | + * [2161] Partition Array According to Given Pivot |
| 3 | + * |
| 4 | + * You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that the following conditions are satisfied: |
| 5 | + * |
| 6 | + * Every element less than pivot appears before every element greater than pivot. |
| 7 | + * Every element equal to pivot appears in between the elements less than and greater than pivot. |
| 8 | + * The relative order of the elements less than pivot and the elements greater than pivot is maintained. |
| 9 | + * |
| 10 | + * More formally, consider every pi, pj where pi is the new position of the i^th element and pj is the new position of the j^th element. If i < j and both elements are smaller (or larger) than pivot, then pi < pj. |
| 11 | + * |
| 12 | + * |
| 13 | + * |
| 14 | + * Return nums after the rearrangement. |
| 15 | + * |
| 16 | + * Example 1: |
| 17 | + * |
| 18 | + * Input: nums = [9,12,5,10,14,3,10], pivot = 10 |
| 19 | + * Output: [9,5,3,10,10,12,14] |
| 20 | + * Explanation: |
| 21 | + * The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array. |
| 22 | + * The elements 12 and 14 are greater than the pivot so they are on the right side of the array. |
| 23 | + * The relative ordering of the elements less than and greater than pivot is also maintained. [9, 5, 3] and [12, 14] are the respective orderings. |
| 24 | + * |
| 25 | + * Example 2: |
| 26 | + * |
| 27 | + * Input: nums = [-3,4,3,2], pivot = 2 |
| 28 | + * Output: [-3,2,4,3] |
| 29 | + * Explanation: |
| 30 | + * The element -3 is less than the pivot so it is on the left side of the array. |
| 31 | + * The elements 4 and 3 are greater than the pivot so they are on the right side of the array. |
| 32 | + * The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings. |
| 33 | + * |
| 34 | + * |
| 35 | + * Constraints: |
| 36 | + * |
| 37 | + * 1 <= nums.length <= 10^5 |
| 38 | + * -10^6 <= nums[i] <= 10^6 |
| 39 | + * pivot equals to an element of nums. |
| 40 | + * |
| 41 | + */ |
| 42 | +pub struct Solution {} |
| 43 | + |
| 44 | +// problem: https://leetcode.com/problems/partition-array-according-to-given-pivot/ |
| 45 | +// discuss: https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/?currentPage=1&orderBy=most_votes&query= |
| 46 | + |
| 47 | +// submission codes start here |
| 48 | + |
| 49 | +impl Solution { |
| 50 | + pub fn pivot_array(nums: Vec<i32>, pivot: i32) -> Vec<i32> { |
| 51 | + let (mut less, mut equal, mut greater) = (Vec::new(), Vec::new(), Vec::new()); |
| 52 | + nums.into_iter().for_each(|num| match num.cmp(&pivot) { |
| 53 | + std::cmp::Ordering::Less => less.push(num), |
| 54 | + std::cmp::Ordering::Equal => equal.push(num), |
| 55 | + std::cmp::Ordering::Greater => greater.push(num), |
| 56 | + }); |
| 57 | + [less, equal, greater].concat() |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// submission codes end |
| 62 | + |
| 63 | +#[cfg(test)] |
| 64 | +mod tests { |
| 65 | + use super::*; |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_2161_example_1() { |
| 69 | + let nums = vec![9, 12, 5, 10, 14, 3, 10]; |
| 70 | + let pivot = 10; |
| 71 | + |
| 72 | + let result = vec![9, 5, 3, 10, 10, 12, 14]; |
| 73 | + |
| 74 | + assert_eq!(Solution::pivot_array(nums, pivot), result); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn test_2161_example_2() { |
| 79 | + let nums = vec![-3, 4, 3, 2]; |
| 80 | + let pivot = 2; |
| 81 | + |
| 82 | + let result = vec![-3, 2, 4, 3]; |
| 83 | + |
| 84 | + assert_eq!(Solution::pivot_array(nums, pivot), result); |
| 85 | + } |
| 86 | +} |
0 commit comments