|
| 1 | +# [2863. Maximum Length of Semi-Decreasing Subarrays](https://leetcode.cn/problems/maximum-length-of-semi-decreasing-subarrays) |
| 2 | + |
| 3 | +[English Version](/solution/2800-2899/2863.Maximum%20Length%20of%20Semi-Decreasing%20Subarrays/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>You are given an integer array <code>nums</code>.</p> |
| 10 | + |
| 11 | +<p>Return <em>the length of the <strong>longest semi-decreasing</strong> subarray of </em><code>nums</code><em>, and </em><code>0</code><em> if there are no such subarrays.</em></p> |
| 12 | + |
| 13 | +<ul> |
| 14 | + <li>A <b>subarray</b> is a contiguous non-empty sequence of elements within an array.</li> |
| 15 | + <li>A non-empty array is <strong>semi-decreasing</strong> if its first element is <strong>strictly greater</strong> than its last element.</li> |
| 16 | +</ul> |
| 17 | + |
| 18 | +<p> </p> |
| 19 | +<p><strong class="example">Example 1:</strong></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | +<strong>Input:</strong> nums = [7,6,5,4,3,2,1,6,10,11] |
| 23 | +<strong>Output:</strong> 8 |
| 24 | +<strong>Explanation:</strong> Take the subarray [7,6,5,4,3,2,1,6]. |
| 25 | +The first element is 7 and the last one is 6 so the condition is met. |
| 26 | +Hence, the answer would be the length of the subarray or 8. |
| 27 | +It can be shown that there aren't any subarrays with the given condition with a length greater than 8. |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p><strong class="example">Example 2:</strong></p> |
| 31 | + |
| 32 | +<pre> |
| 33 | +<strong>Input:</strong> nums = [57,55,50,60,61,58,63,59,64,60,63] |
| 34 | +<strong>Output:</strong> 6 |
| 35 | +<strong>Explanation:</strong> Take the subarray [61,58,63,59,64,60]. |
| 36 | +The first element is 61 and the last one is 60 so the condition is met. |
| 37 | +Hence, the answer would be the length of the subarray or 6. |
| 38 | +It can be shown that there aren't any subarrays with the given condition with a length greater than 6. |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p><strong class="example">Example 3:</strong></p> |
| 42 | + |
| 43 | +<pre> |
| 44 | +<strong>Input:</strong> nums = [1,2,3,4] |
| 45 | +<strong>Output:</strong> 0 |
| 46 | +<strong>Explanation:</strong> Since there are no semi-decreasing subarrays in the given array, the answer is 0. |
| 47 | +</pre> |
| 48 | + |
| 49 | +<p> </p> |
| 50 | +<p><strong>Constraints:</strong></p> |
| 51 | + |
| 52 | +<ul> |
| 53 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 54 | + <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> |
| 55 | +</ul> |
| 56 | + |
| 57 | +## 解法 |
| 58 | + |
| 59 | +<!-- 这里可写通用的实现逻辑 --> |
| 60 | + |
| 61 | +**方法一:哈希表 + 排序** |
| 62 | + |
| 63 | +题目实际上是求逆序对的最大长度,我们不妨用哈希表 $d$ 记录数组中每个数字 $x$ 对应的下标 $i$。 |
| 64 | + |
| 65 | +接下来,我们按照数字从大到小的顺序遍历哈希表的键,用一个数字 $k$ 维护此前出现过的最小的下标,那么对于当前的数字 $x$,我们可以得到一个最大的逆序对长度 $d[x][|d[x]|-1]-k + 1$,其中 $|d[x]|$ 表示数组 $d[x]$ 的长度,即数字 $x$ 在原数组中出现的次数,我们更新答案即可。然后,我们将 $k$ 更新为 $d[x][0]$,即数字 $x$ 在原数组中第一次出现的下标。继续遍历哈希表的键,直到遍历完所有的键。 |
| 66 | + |
| 67 | +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。 |
| 68 | + |
| 69 | +<!-- tabs:start --> |
| 70 | + |
| 71 | +### **Python3** |
| 72 | + |
| 73 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 74 | + |
| 75 | +```python |
| 76 | +class Solution: |
| 77 | + def maxSubarrayLength(self, nums: List[int]) -> int: |
| 78 | + d = defaultdict(list) |
| 79 | + for i, x in enumerate(nums): |
| 80 | + d[x].append(i) |
| 81 | + ans, k = 0, inf |
| 82 | + for x in sorted(d, reverse=True): |
| 83 | + ans = max(ans, d[x][-1] - k + 1) |
| 84 | + k = min(k, d[x][0]) |
| 85 | + return ans |
| 86 | +``` |
| 87 | + |
| 88 | +### **Java** |
| 89 | + |
| 90 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 91 | + |
| 92 | +```java |
| 93 | +class Solution { |
| 94 | + public int maxSubarrayLength(int[] nums) { |
| 95 | + TreeMap<Integer, List<Integer>> d = new TreeMap<>(Comparator.reverseOrder()); |
| 96 | + for (int i = 0; i < nums.length; ++i) { |
| 97 | + d.computeIfAbsent(nums[i], k -> new ArrayList<>()).add(i); |
| 98 | + } |
| 99 | + int ans = 0, k = 1 << 30; |
| 100 | + for (List<Integer> idx : d.values()) { |
| 101 | + ans = Math.max(ans, idx.get(idx.size() - 1) - k + 1); |
| 102 | + k = Math.min(k, idx.get(0)); |
| 103 | + } |
| 104 | + return ans; |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +### **C++** |
| 110 | + |
| 111 | +```cpp |
| 112 | +class Solution { |
| 113 | +public: |
| 114 | + int maxSubarrayLength(vector<int>& nums) { |
| 115 | + map<int, vector<int>, greater<int>> d; |
| 116 | + for (int i = 0; i < nums.size(); ++i) { |
| 117 | + d[nums[i]].push_back(i); |
| 118 | + } |
| 119 | + int ans = 0, k = 1 << 30; |
| 120 | + for (auto& [_, idx] : d) { |
| 121 | + ans = max(ans, idx.back() - k + 1); |
| 122 | + k = min(k, idx[0]); |
| 123 | + } |
| 124 | + return ans; |
| 125 | + } |
| 126 | +}; |
| 127 | +``` |
| 128 | +
|
| 129 | +### **Go** |
| 130 | +
|
| 131 | +```go |
| 132 | +func maxSubarrayLength(nums []int) (ans int) { |
| 133 | + d := map[int][]int{} |
| 134 | + for i, x := range nums { |
| 135 | + d[x] = append(d[x], i) |
| 136 | + } |
| 137 | + keys := []int{} |
| 138 | + for x := range d { |
| 139 | + keys = append(keys, x) |
| 140 | + } |
| 141 | + sort.Slice(keys, func(i, j int) bool { return keys[i] > keys[j] }) |
| 142 | + k := 1 << 30 |
| 143 | + for _, x := range keys { |
| 144 | + idx := d[x] |
| 145 | + ans = max(ans, idx[len(idx)-1]-k+1) |
| 146 | + k = min(k, idx[0]) |
| 147 | + } |
| 148 | + return ans |
| 149 | +} |
| 150 | +
|
| 151 | +func min(a, b int) int { |
| 152 | + if a < b { |
| 153 | + return a |
| 154 | + } |
| 155 | + return b |
| 156 | +} |
| 157 | +
|
| 158 | +func max(a, b int) int { |
| 159 | + if a > b { |
| 160 | + return a |
| 161 | + } |
| 162 | + return b |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +### **TypeScript** |
| 167 | + |
| 168 | +```ts |
| 169 | +function maxSubarrayLength(nums: number[]): number { |
| 170 | + const d: Map<number, number[]> = new Map(); |
| 171 | + for (let i = 0; i < nums.length; ++i) { |
| 172 | + if (!d.has(nums[i])) { |
| 173 | + d.set(nums[i], []); |
| 174 | + } |
| 175 | + d.get(nums[i])!.push(i); |
| 176 | + } |
| 177 | + const keys = Array.from(d.keys()).sort((a, b) => b - a); |
| 178 | + let ans = 0; |
| 179 | + let k = Infinity; |
| 180 | + for (const x of keys) { |
| 181 | + const idx = d.get(x)!; |
| 182 | + ans = Math.max(ans, idx.at(-1) - k + 1); |
| 183 | + k = Math.min(k, idx[0]); |
| 184 | + } |
| 185 | + return ans; |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +### **...** |
| 190 | + |
| 191 | +``` |
| 192 | +
|
| 193 | +``` |
| 194 | + |
| 195 | +<!-- tabs:end --> |
0 commit comments