-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path376. Wiggle Subsequence.ts
44 lines (37 loc) · 1.04 KB
/
376. Wiggle Subsequence.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{/**
* Runtime: 86 ms, faster than 45.45% of TypeScript online submissions for Wiggle Subsequence.
* Memory Usage: 42.8 MB, less than 63.64% of TypeScript online submissions for Wiggle Subsequence.
*/
const POSITIVE = "POSITIVE";
const NEGATIVE = "NEGATIVE";
const NEUTRAL = "NEUTRAL";
const diffrentOfNums = (num1: number, num2: number) => {
if (num1 < num2) {
return POSITIVE;
} else if (num1 > num2) {
return NEGATIVE;
}
return NEUTRAL;
}
function wiggleMaxLength(nums: number[]): number {
if (nums.length === 1) {
return 1;
}
let count = 1;
let currentIndex = 1;
let prevStatus = diffrentOfNums(nums[0], nums[1]);
if (prevStatus !== NEUTRAL) {
count++;
}
for (let i = 2; i < nums.length; i++) {
const currentStatus = diffrentOfNums(nums[currentIndex], nums[i]);
currentIndex = i;
if (prevStatus === currentStatus || currentStatus === NEUTRAL) {
continue;
}
count++;
prevStatus = currentStatus;
}
return count;
};
}