File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -199,6 +199,36 @@ class Solution {
199
199
}
200
200
```
201
201
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
+
202
232
Python:
203
233
``` python3
204
234
class Solution :
You can’t perform that action at this time.
0 commit comments