-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSolution376.java
More file actions
34 lines (28 loc) · 946 Bytes
/
Solution376.java
File metadata and controls
34 lines (28 loc) · 946 Bytes
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
package dynamic_problem;
/**
* 最长公共子序列 LCS
* 1、LCS(m, n) S1[0...m] 和 S2[0...n] 的最长公共子序列的长度。
* S1[m] == S2[n]: LCS(m, n) = 1 + LCS(m-1, n-1)
* S1[m] != S2[n]: LCS(m, n) = max( LCS(m-1, n), LCS(m, n-1))
*
* dijkstra 单源最短路径算法也是动态规划
* 1、shortestPath(i) 为从start到i的最短路径长度。
* shortestPath(x) = min(shortestPath(a) + w(a->x))
*/
public class Solution376 {
public int wiggleMaxLength(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
// 设立一个 up 和 down 变量
int up = 1, down = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] > nums[i - 1]) {
up = down + 1;
} else if (nums[i] < nums[i - 1]) {
down = up + 1;
}
}
return Math.max(up, down);
}
}