diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index d4daccc5..0199d83b 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -479,6 +479,30 @@ impl Solution { } ``` +**动态规划** + +```rust +impl Solution { + pub fn wiggle_max_length(nums: Vec) -> i32 { + if nums.len() == 1 { + return 1; + } + let (mut down, mut up) = (1, 1); + for i in 1..nums.len() { + // i - 1 为峰顶 + if nums[i] < nums[i - 1] { + down = down.max(up + 1); + } + // i - 1 为峰谷 + if nums[i] > nums[i - 1] { + up = up.max(down + 1); + } + } + down.max(up) + } +} +``` + ### C **贪心**