Skip to content

Commit 528472d

Browse files
committed
Sync LeetCode submission Runtime - 30 ms (66.06%), Memory - 34.5 MB (57.71%)
1 parent 7b990c6 commit 528472d

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>pivot</code>. Rearrange <code>nums</code> such that the following conditions are satisfied:</p>
2+
3+
<ul>
4+
<li>Every element less than <code>pivot</code> appears <strong>before</strong> every element greater than <code>pivot</code>.</li>
5+
<li>Every element equal to <code>pivot</code> appears <strong>in between</strong> the elements less than and greater than <code>pivot</code>.</li>
6+
<li>The <strong>relative order</strong> of the elements less than <code>pivot</code> and the elements greater than <code>pivot</code> is maintained.
7+
<ul>
8+
<li>More formally, consider every <code>p<sub>i</sub></code>, <code>p<sub>j</sub></code> where <code>p<sub>i</sub></code> is the new position of the <code>i<sup>th</sup></code> element and <code>p<sub>j</sub></code> is the new position of the <code>j<sup>th</sup></code> element. If <code>i &lt; j</code> and <strong>both</strong> elements are smaller (<em>or larger</em>) than <code>pivot</code>, then <code>p<sub>i</sub> &lt; p<sub>j</sub></code>.</li>
9+
</ul>
10+
</li>
11+
</ul>
12+
13+
<p>Return <code>nums</code><em> after the rearrangement.</em></p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> nums = [9,12,5,10,14,3,10], pivot = 10
20+
<strong>Output:</strong> [9,5,3,10,10,12,14]
21+
<strong>Explanation:</strong>
22+
The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
23+
The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
24+
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.
25+
</pre>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> nums = [-3,4,3,2], pivot = 2
31+
<strong>Output:</strong> [-3,2,4,3]
32+
<strong>Explanation:</strong>
33+
The element -3 is less than the pivot so it is on the left side of the array.
34+
The elements 4 and 3 are greater than the pivot so they are on the right side of the array.
35+
The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
<p><strong>Constraints:</strong></p>
40+
41+
<ul>
42+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
43+
<li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
44+
<li><code>pivot</code> equals to an element of <code>nums</code>.</li>
45+
</ul>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Approach 1: Dynamic Lists
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
8+
less = []
9+
equal = []
10+
greater = []
11+
12+
for num in nums:
13+
if num < pivot:
14+
less.append(num)
15+
elif num > pivot:
16+
greater.append(num)
17+
else:
18+
equal.append(num)
19+
20+
less.extend(equal)
21+
less.extend(greater)
22+
23+
return less
24+

0 commit comments

Comments
 (0)