Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f9abccf

Browse files
authored
Update 0376.摆动序列.md
增加dp方法
1 parent c8cc5b5 commit f9abccf

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

problems/0376.摆动序列.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,36 @@ class Solution {
199199
}
200200
```
201201

202+
```java
203+
// DP
204+
class Solution {
205+
public int wiggleMaxLength(int[] nums) {
206+
// 0 i 作为波峰的最大长度
207+
// 1 i 作为波谷的最大长度
208+
int dp[][] = new int[nums.length][2];
209+
210+
dp[0][0] = dp[0][1] = 1;
211+
for (int i = 1; i < nums.length; i++){
212+
//i 自己可以成为波峰或者波谷
213+
dp[i][0] = dp[i][1] = 1;
214+
215+
for (int j = 0; j < i; j++){
216+
if (nums[j] > nums[i]){
217+
// i 是波谷
218+
dp[i][1] = Math.max(dp[i][1], dp[j][0] + 1);
219+
}
220+
if (nums[j] < nums[i]){
221+
// i 是波峰
222+
dp[i][0] = Math.max(dp[i][0], dp[j][1] + 1);
223+
}
224+
}
225+
}
226+
227+
return Math.max(dp[nums.length - 1][0], dp[nums.length - 1][1]);
228+
}
229+
}
230+
```
231+
202232
Python:
203233
```python3
204234
class Solution:

0 commit comments

Comments
 (0)