Skip to content

Commit 3f7c405

Browse files
committedMar 3, 2025
Add solution #2161
1 parent 1be29d7 commit 3f7c405

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@
662662
2129|[Capitalize the Title](./2129-capitalize-the-title.js)|Easy|
663663
2130|[Maximum Twin Sum of a Linked List](./2130-maximum-twin-sum-of-a-linked-list.js)|Medium|
664664
2154|[Keep Multiplying Found Values by Two](./2154-keep-multiplying-found-values-by-two.js)|Easy|
665+
2161|[Partition Array According to Given Pivot](./2161-partition-array-according-to-given-pivot.js)|Medium|
665666
2185|[Counting Words With a Given Prefix](./2185-counting-words-with-a-given-prefix.js)|Easy|
666667
2215|[Find the Difference of Two Arrays](./2215-find-the-difference-of-two-arrays.js)|Easy|
667668
2235|[Add Two Integers](./2235-add-two-integers.js)|Easy|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 2161. Partition Array According to Given Pivot
3+
* https://leetcode.com/problems/partition-array-according-to-given-pivot/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that
7+
* the following conditions are satisfied:
8+
* - Every element less than pivot appears before every element greater than pivot.
9+
* - Every element equal to pivot appears in between the elements less than and greater than
10+
* pivot.
11+
* - The relative order of the elements less than pivot and the elements greater than pivot
12+
* is maintained.
13+
* - More formally, consider every pi, pj where pi is the new position of the ith element
14+
* and pj is the new position of the jth element. If i < j and both elements are smaller
15+
* (or larger) than pivot, then pi < pj.
16+
*
17+
* Return nums after the rearrangement.
18+
*/
19+
20+
/**
21+
* @param {number[]} nums
22+
* @param {number} pivot
23+
* @return {number[]}
24+
*/
25+
var pivotArray = function(nums, pivot) {
26+
return [
27+
...nums.filter(x => x < pivot),
28+
...nums.filter(x => x === pivot),
29+
...nums.filter(x => x > pivot),
30+
];
31+
};

0 commit comments

Comments
 (0)
Please sign in to comment.