From 30b5b629a288a380f21dcda11d6f8be5d9e17f10 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Mon, 6 Mar 2023 12:24:23 +0800 Subject: [PATCH] =?UTF-8?q?Update=200376.=E6=91=86=E5=8A=A8=E5=BA=8F?= =?UTF-8?q?=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0376.摆动序列.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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 **贪心**