Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6098841

Browse files
committedFeb 21, 2025
Add solution #324
1 parent f6d141a commit 6098841

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@
257257
318|[Maximum Product of Word Lengths](./0318-maximum-product-of-word-lengths.js)|Medium|
258258
319|[Bulb Switcher](./0319-bulb-switcher.js)|Medium|
259259
322|[Coin Change](./0322-coin-change.js)|Medium|
260+
324|[Wiggle Sort II](./0324-wiggle-sort-ii.js)|Medium|
260261
326|[Power of Three](./0326-power-of-three.js)|Easy|
261262
328|[Odd Even Linked List](./0328-odd-even-linked-list.js)|Medium|
262263
329|[Longest Increasing Path in a Matrix](./0329-longest-increasing-path-in-a-matrix.js)|Hard|

‎solutions/0324-wiggle-sort-ii.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 324. Wiggle Sort II
3+
* https://leetcode.com/problems/wiggle-sort-ii/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]
7+
*
8+
* You may assume the input array always has a valid answer.
9+
*/
10+
11+
/**
12+
* @param {number[]} nums
13+
* @return {void} Do not return anything, modify nums in-place instead.
14+
*/
15+
var wiggleSort = function(nums) {
16+
nums.sort((a, b) => a - b);
17+
const m = Math.floor((nums.length - 1) / 2);
18+
for (let i = 0, l = m, r = nums.length - 1, t = [...nums]; i < nums.length; i++) {
19+
nums[i] = i % 2 ? t[r--] : t[l--];
20+
}
21+
};

0 commit comments

Comments
 (0)
Please sign in to comment.