Skip to content

Commit aa159fc

Browse files
committed
Time: 0 ms (100%), Space: 52.8 MB (5.48%) - LeetHub
1 parent 160a2ea commit aa159fc

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var wiggleMaxLength = function (nums) {
6+
/* let nums = [1,17,5,10,13,15,10,5,16,8]
7+
Greedy Algorithm
8+
*/
9+
if (nums.length <= 1) return nums.length;
10+
let prevDiff = 0;
11+
let result = 1;
12+
for (let i = 0; i < nums.length - 1; i++) {
13+
let curDiff = nums[i + 1] - nums[i];
14+
if (prevDiff <= 0 && curDiff > 0) {
15+
result++;
16+
prevDiff = curDiff;
17+
} else if (prevDiff >= 0 && curDiff < 0) {
18+
result++;
19+
prevDiff = curDiff;
20+
}
21+
}
22+
return result;
23+
};

0 commit comments

Comments
 (0)