• LeetCode 2760. 最长奇偶子数组:模拟(使用一个变量记录状态)


    【LetMeFly】2760.最长奇偶子数组:模拟(使用一个变量记录状态)

    力扣题目链接:https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/

    给你一个下标从 0 开始的整数数组 nums 和一个整数 threshold

    请你从 nums 的子数组中找出以下标 l 开头、下标 r 结尾 (0 <= l <= r < nums.length) 且满足以下条件的 最长子数组

    • nums[l] % 2 == 0
    • 对于范围 [l, r - 1] 内的所有下标 inums[i] % 2 != nums[i + 1] % 2
    • 对于范围 [l, r] 内的所有下标 inums[i] <= threshold

    以整数形式返回满足题目要求的最长子数组的长度。

    注意:子数组 是数组中的一个连续非空元素序列。

     

    示例 1:

    输入:nums = [3,2,5,4], threshold = 5
    输出:3
    解释:在这个示例中,我们选择从 l = 1 开始、到 r = 3 结束的子数组 => [2,5,4] ,满足上述条件。
    因此,答案就是这个子数组的长度 3 。可以证明 3 是满足题目要求的最大长度。

    示例 2:

    输入:nums = [1,2], threshold = 2
    输出:1
    解释:
    在这个示例中,我们选择从 l = 1 开始、到 r = 1 结束的子数组 => [2] 。
    该子数组满足上述全部条件。可以证明 1 是满足题目要求的最大长度。
    

    示例 3:

    输入:nums = [2,3,4,5], threshold = 4
    输出:3
    解释:
    在这个示例中,我们选择从 l = 0 开始、到 r = 2 结束的子数组 => [2,3,4] 。 
    该子数组满足上述全部条件。
    因此,答案就是这个子数组的长度 3 。可以证明 3 是满足题目要求的最大长度。

     

    提示:

    • 1 <= nums.length <= 100
    • 1 <= nums[i] <= 100
    • 1 <= threshold <= 100

    方法一:模拟(使用一个变量记录状态)

    使用变量 n o w C n t nowCnt nowCnt来记录当前“奇偶子数组”的长度。

    • 如果 n o w C n t ≠ 0 nowCnt \neq 0 nowCnt=0,说明当前元素前面是“奇偶子数组”,因此看当前元素能否加入到子数组中。
      • 如果当前元素 ≤ t h r e s h o l d \le threshold threshold并且当前元素奇偶性和上一个元素不同,则 n o w C n t + + nowCnt++ nowCnt++
      • 否则,更新答案 a n s ans ans最大值,并将 n o w C n t nowCnt nowCnt归零
    • 否则( n o w C n t nowCnt nowCnt为零),则看当前元素能否成为“奇偶子数组”的开始(为偶、不大于 t h r e s h o l d threshold threshold

    注意对 n o w C n t ≠ 0 nowCnt \neq 0 nowCnt=0的判断要在 n o w C n t = 0 nowCnt=0 nowCnt=0的判断之前,因为偶数元素可能无法添加到数组末尾但是可以作为数组的开头。

    • 时间复杂度 O ( l e n ( n u m s ) ) O(len(nums)) O(len(nums))
    • 空间复杂度 O ( 1 ) O(1) O(1)

    AC代码

    C++
    class Solution {
    public:
        int longestAlternatingSubarray(vector<int>& nums, int threshold) {
            int ans = 0;
            int nowCnt = 0;
            for (int i = 0; i < nums.size(); i++) {
                if (nowCnt) {
                    if (nums[i] <= threshold && nums[i] % 2 != nums[i - 1] % 2) {
                        nowCnt++;
                    }
                    else {
                        ans = max(ans, nowCnt);
                        nowCnt = 0;
                    }
                }
                if (!nowCnt && nums[i] <= threshold && nums[i] % 2 == 0) {
                    nowCnt = 1;
                }
            }
            return max(ans, nowCnt);
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    Python
    # from typing import List
    
    class Solution:
        def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:
            ans = 0
            nowCnt = 0
            for i in range(len(nums)):
                if nowCnt:
                    if nums[i] <= threshold and nums[i] % 2 != nums[i - 1] % 2:
                        nowCnt += 1
                    else:
                        ans = max(ans, nowCnt)
                        nowCnt = 0
                if not nowCnt:
                    if nums[i] <= threshold and nums[i] % 2 == 0:
                        nowCnt = 1
            return max(ans, nowCnt)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    同步发文于CSDN,原创不易,转载经作者同意后请附上原文链接哦~
    Tisfy:https://letmefly.blog.csdn.net/article/details/134449952

  • 相关阅读:
    Autosar诊断实战系列21-UDS连续帧(CF)数据接收代码级分析
    图解Intel SM4-AES-NI实现方案
    Scala入门
    【FastDFS】一文学会一个分布式文件系统!
    20 mysql const 查询
    轻量级简约仪表板Dasherr
    RT-Thread 组件 FinSH 使用时遇到的问题
    优酷视频元素内容召回系统:多级多模态引擎探索
    第二届 Oceanbase 开发者大会 实录
    秋招我借这份PDF的复习思路,收获美团,小米,京东等Java岗offer
  • 原文地址:https://blog.csdn.net/Tisfy/article/details/134449952