|
| 1 | +### 题目描述 |
| 2 | + |
| 3 | +这是 LeetCode 上的 **[2698. 求一个整数的惩罚数]()** ,难度为 **简单**。 |
| 4 | + |
| 5 | +Tag : 「双指针」 |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +给你一个下标从 $0$ 开始的整数数组 `nums` 和一个整数 `threshold`。 |
| 10 | + |
| 11 | +请你从 `nums` 的子数组中找出以下标 `l` 开头、下标 `r` 结尾 ($0 <= l <= r < nums.length$) 且满足以下条件的 最长子数组 : |
| 12 | + |
| 13 | +* `nums[l] % 2 == 0` |
| 14 | +* 对于范围 $[l, r - 1]$ 内的所有下标 `i`,`nums[i] % 2 != nums[i + 1] % 2` |
| 15 | +* 对于范围 $[l, r]$ 内的所有下标 `i`,`nums[i] <= threshold` |
| 16 | + |
| 17 | +以整数形式返回满足题目要求的最长子数组的长度。 |
| 18 | + |
| 19 | +注意:子数组 是数组中的一个连续非空元素序列。 |
| 20 | + |
| 21 | +示例 1: |
| 22 | +``` |
| 23 | +输入:nums = [3,2,5,4], threshold = 5 |
| 24 | +
|
| 25 | +输出:3 |
| 26 | +
|
| 27 | +解释:在这个示例中,我们选择从 l = 1 开始、到 r = 3 结束的子数组 => [2,5,4] ,满足上述条件。 |
| 28 | +因此,答案就是这个子数组的长度 3 。可以证明 3 是满足题目要求的最大长度。 |
| 29 | +``` |
| 30 | +示例 2: |
| 31 | +``` |
| 32 | +输入:nums = [1,2], threshold = 2 |
| 33 | +
|
| 34 | +输出:1 |
| 35 | +
|
| 36 | +解释: |
| 37 | +在这个示例中,我们选择从 l = 1 开始、到 r = 1 结束的子数组 => [2] 。 |
| 38 | +该子数组满足上述全部条件。可以证明 1 是满足题目要求的最大长度。 |
| 39 | +``` |
| 40 | +示例 3: |
| 41 | +``` |
| 42 | +输入:nums = [2,3,4,5], threshold = 4 |
| 43 | +
|
| 44 | +输出:3 |
| 45 | +
|
| 46 | +解释: |
| 47 | +在这个示例中,我们选择从 l = 0 开始、到 r = 2 结束的子数组 => [2,3,4] 。 |
| 48 | +该子数组满足上述全部条件。 |
| 49 | +因此,答案就是这个子数组的长度 3 。可以证明 3 是满足题目要求的最大长度。 |
| 50 | +``` |
| 51 | + |
| 52 | +提示: |
| 53 | +* $1 <= nums.length <= 100$ |
| 54 | +* $1 <= nums[i] <= 100$ |
| 55 | +* $1 <= threshold <= 100$ |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +### 双指针 |
| 60 | + |
| 61 | +整体题意:求 `nums` 中的最长的子数组 $[l, r]$,对于任意 $nums[i]$ 不超过 `threshold`,且从 $nums[l]$ 开始按照「先偶后奇」顺序交替。 |
| 62 | + |
| 63 | +容易想到「双指针」做法: |
| 64 | + |
| 65 | +* 变量 `i` 作为当前子数组左端点,从前往后扫描 `nums`,首先确保 `i` 的合法性(跳过不满足 `nums[i] % 2 = 0` 和 `nums[i] <= threshold` 的位置);随后在固定左端点 `i` 前提下,找最远的右端点 `j`(值不超过 `threshold`,且奇偶性与前值交替) |
| 66 | +* 得到当前连续段长度,更新 `ans`,从当前结束位置开始,重复上述过程,直到处理完 `nums` |
| 67 | + |
| 68 | +Java 代码 |
| 69 | + |
| 70 | +```Java |
| 71 | +class Solution { |
| 72 | + public int longestAlternatingSubarray(int[] nums, int threshold) { |
| 73 | + int n = nums.length, ans = 0, i = 0; |
| 74 | + while (i < n) { |
| 75 | + if ((nums[i] % 2 != 0 || nums[i] > threshold) && ++i >= 0) continue; |
| 76 | + int j = i + 1, cur = nums[i] % 2; |
| 77 | + while (j < n) { |
| 78 | + if (nums[j] > threshold || nums[j] % 2 == cur) break; |
| 79 | + cur = nums[j] % 2; j++; |
| 80 | + } |
| 81 | + ans = Math.max(ans, j - i); |
| 82 | + i = j; |
| 83 | + } |
| 84 | + return ans; |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | +C++ 代码 |
| 89 | +```C++ |
| 90 | +class Solution { |
| 91 | +public: |
| 92 | + int longestAlternatingSubarray(vector<int>& nums, int threshold) { |
| 93 | + int n = nums.size(), ans = 0, i = 0; |
| 94 | + while (i < n) { |
| 95 | + if ((nums[i] % 2 != 0 || nums[i] > threshold) && ++i >= 0) continue; |
| 96 | + int j = i + 1, cur = nums[i] % 2; |
| 97 | + while (j < n) { |
| 98 | + if (nums[j] > threshold || nums[j] % 2 == cur) break; |
| 99 | + cur = nums[j] % 2; j++; |
| 100 | + } |
| 101 | + ans = max(ans, j - i); |
| 102 | + i = j; |
| 103 | + } |
| 104 | + return ans; |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +``` |
| 109 | +Python 代码 |
| 110 | +```Python |
| 111 | +class Solution: |
| 112 | + def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int: |
| 113 | + n, ans, i = len(nums), 0, 0 |
| 114 | + while i < n: |
| 115 | + if nums[i] % 2 != 0 or nums[i] > threshold: |
| 116 | + i += 1 |
| 117 | + continue |
| 118 | + j, cur = i + 1, nums[i] % 2 |
| 119 | + while j < n: |
| 120 | + if nums[j] > threshold or nums[j] % 2 == cur: break |
| 121 | + cur, j = nums[j] % 2, j + 1 |
| 122 | + ans = max(ans, j - i) |
| 123 | + i = j |
| 124 | + return ans |
| 125 | +``` |
| 126 | +TypeScript 代码 |
| 127 | +```TypeScript |
| 128 | +function longestAlternatingSubarray(nums: number[], threshold: number): number { |
| 129 | + let n = nums.length, ans = 0, i = 0 |
| 130 | + while (i < n) { |
| 131 | + if ((nums[i] % 2 != 0 || nums[i] > threshold) && ++i >= 0) continue; |
| 132 | + let j = i + 1, cur = nums[i] % 2; |
| 133 | + while (j < n) { |
| 134 | + if (nums[j] > threshold || nums[j] % 2 == cur) break; |
| 135 | + cur = nums[j] % 2; j++; |
| 136 | + } |
| 137 | + ans = Math.max(ans, j - i); |
| 138 | + i = j; |
| 139 | + } |
| 140 | + return ans; |
| 141 | +}; |
| 142 | +``` |
| 143 | +* 时间复杂度:$O(n)$ |
| 144 | +* 空间复杂度:$O(1)$ |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +### 最后 |
| 149 | + |
| 150 | +这是我们「刷穿 LeetCode」系列文章的第 `No.2760` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 |
| 151 | + |
| 152 | +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 |
| 153 | + |
| 154 | +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 |
| 155 | + |
| 156 | +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 |
| 157 | + |
0 commit comments