forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_376.java
50 lines (47 loc) · 1.67 KB
/
_376.java
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
45
46
47
48
49
50
package com.fishercoder.solutions;
public class _376 {
public static class Solution1 {
/**
* credit: https://leetcode.com/problems/wiggle-subsequence/discuss/84843/Easy-understanding-DP-solution-with-O(n)-Java-version
*/
public int wiggleMaxLength(int[] nums) {
int len = nums.length;
int[] up = new int[len];
int[] down = new int[len];
up[0] = 1;
down[0] = 1;
for (int i = 1; i < len; i++) {
if (nums[i] > nums[i - 1]) {
up[i] = down[i - 1] + 1;
down[i] = down[i - 1];
} else if (nums[i] < nums[i - 1]) {
down[i] = up[i - 1] + 1;
up[i] = up[i - 1];
} else {
up[i] = up[i - 1];
down[i] = down[i - 1];
}
}
return Math.max(down[len - 1], up[len - 1]);
}
}
public static class Solution2 {
public int wiggleMaxLength(int[] nums) {
if (nums.length < 2) {
return nums.length;
}
int prevDiff = nums[1] - nums[0];
int count = (prevDiff != 0) ? 2 : 1;
for (int i = 2; i < nums.length; i++) {
int diff = nums[i] - nums[i - 1];
/**ATTN: prevDiff could be zero. e.g. [3,3,3,2,5]
* but diff needs to be exactly greater than zero*/
if ((prevDiff <= 0 && diff > 0) || (prevDiff >= 0) && diff < 0) {
count++;
prevDiff = diff;
}
}
return count;
}
}
}