leetCode 376. Swing sequence dynamic programming + diagram + state transfer

376. Swing sequence - LeetCode

If the difference between consecutive numbers alternates strictly between positive and negative numbers, the sequence of numbers is called  a wobble sequence. The first difference (if present) may be positive or negative. Sequences with only one element or two unequal elements are also considered wobble sequences.

  • For example,  [1, 7, 4, 9, 2, 5] it is a  swing sequence  because the differences  (6, -3, 5, -7, 3) alternate between positive and negative values.

  • In contrast, the sum is [1, 4, 7, 2, 5] not  [1, 7, 4, 5, 5] a oscillating sequence, the first one because its first two differences are both positive, and the second one because its last difference is zero.

A subsequence  can be obtained by deleting some (or not) elements from the original sequence, leaving the remaining elements in their original order.

Given an array of integers  nums , return  the length of the longest subsequencenums  in  the wobble  sequence   .

Example 1:

Input: nums = [1,7,4,9,2,5]
 Output: 6
 Explanation: The entire sequence is a swing sequence, and the difference between each element is (6, -3, 5, -7, 3) .

Example 2:

Input: nums = [1,17,5,10,13,15,10,5,16,8]
 Output: 7
 Explanation: This sequence contains several wobble sequences of length 7. 
One of them is [1, 17, 10, 13, 10, 16, 8], and the difference between the elements is (16, -7, 3, -3, 6, -8).

Example 3:

Input: nums = [1,2,3,4,5,6,7,8,9]
 Output: 2

>>Ideas and analysis

Official solution 376. Swinging sequence - LeetCode and this article 376. Swinging sequence - LeetCode

Summary and summary of the ideas for solving the given problem

  • An element in a sequence is called a "peak" if and only if the adjacent elements on both sides of the element are smaller than it. For example, 3 in the sequence [1,3,2,4] is a "peak"
  • An element in a sequence is called a "valley" if and only if the adjacent elements on both sides of the element are larger than it. For example, 2 in the sequence [1,3,2,4] is a "valley"
  • In particular, for elements located at both ends of the sequence, only the adjacent elements on one side are smaller or larger than it, which is also called a "peak" or "valley" . For example , in the sequence [1,3,2,4] , 1 is also a "valley" and 4 is a "peak"

Status definition:

  • up[i]: The longest rising swing sequence in the array nums[0...i]
  • down[i]: the longest descending swing sequence in the array nums[0...i]

State transfer:

  • nums[i] > nums[i-1]: up[i] = down[i-1] + 1; down[i] = down[i-1]
  • nums[i] < nums[i-1]: up[i] = up[i-1]; down[i] = up[i-1] + 1

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        int n = nums.size();
        if (n < 2) {
            return n;
        }
        int up = 1, down = 1;
        for (int i = 1; i < n; i++) {
            if (nums[i] > nums[i - 1]) {
                up = down + 1;
            } else if (nums[i] < nums[i - 1]) {
                down = up + 1;
            }
        }
        return max(up, down);
    }
};

// 作者:力扣官方题解
// 链接:https://leetcode.cn/problems/wiggle-subsequence/solutions/805292/python3-yi-tu-sheng-qian-yan-by-v12de-ao-72b1/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

My previous articles: leetCode 376. Swinging Sequence Greedy Algorithm_Heheda( ̄▽ ̄)"'s blog-CSDN blog

recommended article:

376. Swing sequence - LeetCode

376. Swing sequence - LeetCode 

Guess you like

Origin blog.csdn.net/weixin_41987016/article/details/133470576