|
| 1 | +--- |
| 2 | +title: 3255.长度为 K 的子数组的能量值 II |
| 3 | +date: 2024-11-07 12:35:10 |
| 4 | +tags: [题解, LeetCode, 中等, 数组, 滑动窗口, 遍历] |
| 5 | +--- |
| 6 | + |
| 7 | +# 【LetMeFly】3255.长度为 K 的子数组的能量值 II:和官解思路不同的O(n)做法(附思考过程) |
| 8 | + |
| 9 | +力扣题目链接:[https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-ii/](https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-ii/) |
| 10 | + |
| 11 | +<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code> 和一个正整数 <code>k</code> 。</p> |
| 12 | + |
| 13 | +<p>一个数组的 <strong>能量值</strong> 定义为:</p> |
| 14 | + |
| 15 | +<ul> |
| 16 | + <li>如果 <strong>所有</strong> 元素都是依次 <strong>连续</strong> 且 <strong>上升</strong> 的,那么能量值为 <strong>最大</strong> 的元素。</li> |
| 17 | + <li>否则为 -1 。</li> |
| 18 | +</ul> |
| 19 | + |
| 20 | +<p>你需要求出 <code>nums</code> 中所有长度为 <code>k</code> 的 <span data-keyword="subarray-nonempty">子数组</span> 的能量值。</p> |
| 21 | + |
| 22 | +<p>请你返回一个长度为 <code>n - k + 1</code> 的整数数组 <code>results</code> ,其中 <code>results[i]</code> 是子数组 <code>nums[i..(i + k - 1)]</code> 的能量值。</p> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | + |
| 26 | +<p><strong class="example">示例 1:</strong></p> |
| 27 | + |
| 28 | +<div class="example-block"> |
| 29 | +<p><span class="example-io"><b>输入:</b>nums = [1,2,3,4,3,2,5], k = 3</span></p> |
| 30 | + |
| 31 | +<p><b>输出:</b>[3,4,-1,-1,-1]</p> |
| 32 | + |
| 33 | +<p><strong>解释:</strong></p> |
| 34 | + |
| 35 | +<p><code>nums</code> 中总共有 5 个长度为 3 的子数组:</p> |
| 36 | + |
| 37 | +<ul> |
| 38 | + <li><code>[1, 2, 3]</code> 中最大元素为 3 。</li> |
| 39 | + <li><code>[2, 3, 4]</code> 中最大元素为 4 。</li> |
| 40 | + <li><code>[3, 4, 3]</code> 中元素 <strong>不是</strong> 连续的。</li> |
| 41 | + <li><code>[4, 3, 2]</code> 中元素 <b>不是</b> 上升的。</li> |
| 42 | + <li><code>[3, 2, 5]</code> 中元素 <strong>不是</strong> 连续的。</li> |
| 43 | +</ul> |
| 44 | +</div> |
| 45 | + |
| 46 | +<p><strong class="example">示例 2:</strong></p> |
| 47 | + |
| 48 | +<div class="example-block"> |
| 49 | +<p><span class="example-io"><b>输入:</b>nums = [2,2,2,2,2], k = 4</span></p> |
| 50 | + |
| 51 | +<p><span class="example-io"><b>输出:</b>[-1,-1]</span></p> |
| 52 | +</div> |
| 53 | + |
| 54 | +<p><strong class="example">示例 3:</strong></p> |
| 55 | + |
| 56 | +<div class="example-block"> |
| 57 | +<p><span class="example-io"><b>输入:</b>nums = [3,2,3,2,3,2], k = 2</span></p> |
| 58 | + |
| 59 | +<p><span class="example-io"><b>输出:</b>[-1,3,-1,3,-1]</span></p> |
| 60 | +</div> |
| 61 | + |
| 62 | +<p> </p> |
| 63 | + |
| 64 | +<p><strong>提示:</strong></p> |
| 65 | + |
| 66 | +<ul> |
| 67 | + <li><code>1 <= n == nums.length <= 10<sup>5</sup></code></li> |
| 68 | + <li><code>1 <= nums[i] <= 10<sup>6</sup></code></li> |
| 69 | + <li><code>1 <= k <= n</code></li> |
| 70 | +</ul> |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +## 解题方法:遍历(类似滑动窗口) |
| 75 | + |
| 76 | +本题`长度为 K 的子数组的能量值 II`和上一题[`长度为 K 的子数组的能量值 I`](https://blog.letmefly.xyz/2024/11/06/LeetCode%203254.%E9%95%BF%E5%BA%A6%E4%B8%BAK%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E8%83%BD%E9%87%8F%E5%80%BCI/)的唯一区别是数据量,本题数据量不支持$O(nk)$时间复杂度的做法。 |
| 77 | + |
| 78 | +上一题中我们枚举每个长度为K的区间的左端点,之后遍历区间,若区间中存在“相邻不连续”的两个元素则能量值为-1,否则为区间的最后一个元素。每个区间都可能需要$O(k)$的时间复杂度来完成结果的计算。 |
| 79 | + |
| 80 | +但是其实不难发现,区间(假设k很大区间很长)每次向右移动一个元素,区间中大量的元素是不变的,只有区间起始位置元素和结束位置元素发生了变化。 |
| 81 | + |
| 82 | +因此解决方案来了,我们使用一个额外的变量$notContinue$记录区间中不连续的“相邻两个元素”有多少对,这样在区间移动的时候只需要根据区间开头元素和末尾元素的变化情况就能在$O(1)$的时间内算出新区间的“notContinue”。 |
| 83 | + |
| 84 | +如果一个区间的$notCoutinue$非零,则说明该区间中存在相邻不连续的元素对,区间“能量值”为$-1$;否则区间能量值为区间的最后一个元素(即最大元素)。 |
| 85 | + |
| 86 | ++ 时间复杂度$O(n)$ |
| 87 | ++ 空间复杂度$O(1)$,力扣返回值不计入算法空间复杂度 |
| 88 | + |
| 89 | +### AC代码 |
| 90 | + |
| 91 | +#### C++ |
| 92 | + |
| 93 | +```cpp |
| 94 | +class Solution { |
| 95 | +public: |
| 96 | + vector<int> resultsArray(vector<int>& nums, int k) { |
| 97 | + vector<int> ans(nums.size() - k + 1); |
| 98 | + int notContinue = 0; |
| 99 | + for (int i = 1; i < k; i++) { |
| 100 | + notContinue += nums[i] != nums[i - 1] + 1; |
| 101 | + } |
| 102 | + ans[0] = notContinue ? -1 : nums[k - 1]; |
| 103 | + for (int i = 1; i + k <= nums.size(); i++) { |
| 104 | + notContinue -= nums[i] != nums[i - 1] + 1; |
| 105 | + notContinue += nums[i + k - 1] != nums[i + k - 2] + 1; |
| 106 | + ans[i] = notContinue ? -1 : nums[i + k - 1]; |
| 107 | + } |
| 108 | + return ans; |
| 109 | + } |
| 110 | +}; |
| 111 | +``` |
| 112 | +
|
| 113 | +#### Python |
| 114 | +
|
| 115 | +```python |
| 116 | +from typing import List |
| 117 | +
|
| 118 | +class Solution: |
| 119 | + def resultsArray(self, nums: List[int], k: int) -> List[int]: |
| 120 | + ans = [0] * (len(nums) - k + 1) |
| 121 | + notCoutinue = sum(nums[i] != nums[i - 1] + 1 for i in range(1, k)) |
| 122 | + ans[0] = -1 if notCoutinue else nums[k - 1] |
| 123 | + for i in range(1, len(nums) - k + 1): |
| 124 | + notCoutinue -= nums[i] != nums[i - 1] + 1 |
| 125 | + notCoutinue += nums[i + k - 1] != nums[i + k - 2] + 1 |
| 126 | + ans[i] = -1 if notCoutinue else nums[i + k - 1] |
| 127 | + return ans |
| 128 | +``` |
| 129 | + |
| 130 | +#### Java |
| 131 | + |
| 132 | +```java |
| 133 | +class Solution { |
| 134 | + public int[] resultsArray(int[] nums, int k) { |
| 135 | + int[] ans = new int[nums.length - k + 1]; |
| 136 | + int notCoutinue = 0; |
| 137 | + for (int i = 1; i < k; i++) { |
| 138 | + if (nums[i] != nums[i - 1] + 1) { |
| 139 | + notCoutinue++; |
| 140 | + } |
| 141 | + } |
| 142 | + ans[0] = notCoutinue > 0 ? -1 : nums[k - 1]; |
| 143 | + for (int i = 1; i + k <= nums.length; i++) { |
| 144 | + if (nums[i] != nums[i - 1] + 1) { |
| 145 | + notCoutinue--; |
| 146 | + } |
| 147 | + if (nums[i + k - 1] != nums[i + k - 2] + 1) { |
| 148 | + notCoutinue++; |
| 149 | + } |
| 150 | + ans[i] = notCoutinue > 0 ? -1 : nums[i + k - 1]; |
| 151 | + } |
| 152 | + return ans; |
| 153 | + } |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +#### Go |
| 158 | + |
| 159 | +```go |
| 160 | +package main |
| 161 | + |
| 162 | +func resultsArray(nums []int, k int) (ans []int) { |
| 163 | + ans = make([]int, len(nums) - k + 1) |
| 164 | + notContinue := 0 |
| 165 | + for i := 1; i < k; i++ { |
| 166 | + if nums[i] != nums[i - 1] + 1 { |
| 167 | + notContinue++ |
| 168 | + } |
| 169 | + } |
| 170 | + if notContinue > 0 { |
| 171 | + ans[0] = -1 |
| 172 | + } else { |
| 173 | + ans[0] = nums[k - 1] |
| 174 | + } |
| 175 | + for i := 1; i + k <= len(nums); i++ { |
| 176 | + if nums[i] != nums[i - 1] + 1 { |
| 177 | + notContinue-- |
| 178 | + } |
| 179 | + if nums[i + k - 1] != nums[i + k - 2] + 1 { |
| 180 | + notContinue++ |
| 181 | + } |
| 182 | + if notContinue > 0 { |
| 183 | + ans[i] = -1 |
| 184 | + } else { |
| 185 | + ans[i] = nums[i + k - 1] |
| 186 | + } |
| 187 | + } |
| 188 | + return |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/11/07/LeetCode%203255.%E9%95%BF%E5%BA%A6%E4%B8%BAK%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E8%83%BD%E9%87%8F%E5%80%BCII/)哦~ |
| 193 | +> |
| 194 | +> Tisfy:[https://letmefly.blog.csdn.net/article/details/143591327](https://letmefly.blog.csdn.net/article/details/143591327) |
0 commit comments