Skip to content

Commit d28cd66

Browse files
committed
[LeetCode Sync] Runtime - 7 ms (52.69%), Memory - 19.4 MB (35.12%)
1 parent ceaaee1 commit d28cd66

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<p>Given an integer array <code>nums</code>, reorder it such that <code>nums[0] &lt; nums[1] &gt; nums[2] &lt; nums[3]...</code>.</p>
2+
3+
<p>You may assume the input array always has a valid answer.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> nums = [1,5,1,1,6,4]
10+
<strong>Output:</strong> [1,6,1,5,1,4]
11+
<strong>Explanation:</strong> [1,4,1,5,1,6] is also accepted.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [1,3,2,2,3,1]
18+
<strong>Output:</strong> [2,3,1,3,1,2]
19+
</pre>
20+
21+
<p>&nbsp;</p>
22+
<p><strong>Constraints:</strong></p>
23+
24+
<ul>
25+
<li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>4</sup></code></li>
26+
<li><code>0 &lt;= nums[i] &lt;= 5000</code></li>
27+
<li>It is guaranteed that there will be an answer for the given input <code>nums</code>.</li>
28+
</ul>
29+
30+
<p>&nbsp;</p>
31+
<strong>Follow Up:</strong> Can you do it in <code>O(n)</code> time and/or <strong>in-place</strong> with <code>O(1)</code> extra space?
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def wiggleSort(self, nums: List[int]) -> None:
3+
"""
4+
Do not return anything, modify nums in-place instead.
5+
"""
6+
temp = sorted(nums)
7+
n = len(temp)
8+
i, j = (n - 1) >> 1, n - 1
9+
for k in range(n):
10+
if k % 2 == 0:
11+
nums[k] = temp[i]
12+
i -= 1
13+
else:
14+
nums[k] = temp[j]
15+
j -= 1

0 commit comments

Comments
 (0)