---
# System prepended metadata

title: 0376. Wiggle Subsequence
tags: [Leetcode, Dynamic Programming, Medium]

---

# 0376. Wiggle Subsequence
###### tags: `Leetcode` `Medium` `Dynamic Programming`
Link: https://leetcode.com/problems/wiggle-subsequence/
## 思路
![](https://i.imgur.com/G3qUVVx.png)
设计两个变量 分别表示以当前元素结尾上升/下降的最长wiggle pattern
对于新的元素```nums[i]```来说
如果```nums[i-1]>nums[i]``` 说明我们可以更新decrease的长度变成increase+1
但是为什么increase的长度是保持不变的呢 因为我们可以把末尾元素由```nums[i-1]```变成```nums[i]```
如果```nums[i-1]>nums[i]``` 则同理
## Code
```java=
class Solution {
    public int wiggleMaxLength(int[] nums) {
        int increase = 1, decrease = 1;
        for(int i=1; i<nums.length; i++){
            if(nums[i]>nums[i-1]) decrease = increase+1;
            else if(nums[i]<nums[i-1]) increase = decrease+1;
        }
        return Math.max(increase, decrease);
    }
}
}
```