Skip to content

Commit 6dd1267

Browse files
committed
update: 376-2
1 parent 72885e3 commit 6dd1267

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/wiggle-subsequence/res.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,25 @@ function wiggleMaxLength(nums: number[]): number {
1010
}
1111

1212
return Math.max(up, down);
13-
};
13+
};
14+
15+
function wiggleMaxLength2(nums: number[]): number {
16+
if (nums.length < 2) {
17+
return nums.length;
18+
}
19+
20+
let currentDiff = 0;
21+
let previousDiff = 0;
22+
let result = 0;
23+
24+
for (let i = 1; i < nums.length; i++) {
25+
currentDiff = nums[i] - nums[i-1];
26+
27+
if (currentDiff>0 && previousDiff<=0 || currentDiff<0 && previousDiff>=0) {
28+
previousDiff = currentDiff;
29+
result++;
30+
}
31+
}
32+
33+
return result+1;
34+
}

0 commit comments

Comments
 (0)