Every day a Leetcode
题目来源:3201. 找出有效子序列的最大长度 I
解法1:枚举
全奇数、全偶数、奇偶交替三种情况的最大值即为所求。
代码:
            
            
              c
              
              
            
          
          /*
 * @lc app=leetcode.cn id=3201 lang=cpp
 *
 * [3201] 找出有效子序列的最大长度 I
 */
// @lc code=start
class Solution
{
public:
    int maximumLength(vector<int> &nums)
    {
        // 分三种情况:全奇、全偶、奇偶交替
        int odd = 0, even = 0;
        for (int &num : nums)
        {
            if (num % 2)
                odd++;
            else
                even++;
        }
        int interlaced = 1, tag = nums[0];
        for (int i = 1; i < nums.size(); i++)
            if ((nums[i] & 1) ^ (tag & 1))
            {
                interlaced++;
                tag = nums[i];
            }
        return max(interlaced, max(odd, even));
    }
};
// @lc code=end
        结果:

复杂度分析:
时间复杂度:O(n),其中 n 是数组 nums 的长度。
空间复杂度:O(1)。