Skip to content

Commit 8d36843

Browse files
authored
feat: add solutions to lc problem: No.2863 (#1656)
No.2863.Maximum Length of Semi-Decreasing Subarrays
1 parent 516cacd commit 8d36843

File tree

11 files changed

+467
-0
lines changed

11 files changed

+467
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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>&nbsp;</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&#39;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&#39;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>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
54+
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 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 -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# [2863. Maximum Length of Semi-Decreasing Subarrays](https://leetcode.com/problems/maximum-length-of-semi-decreasing-subarrays)
2+
3+
[中文文档](/solution/2800-2899/2863.Maximum%20Length%20of%20Semi-Decreasing%20Subarrays/README.md)
4+
5+
## Description
6+
7+
<p>You are given an integer array <code>nums</code>.</p>
8+
9+
<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>
10+
11+
<ul>
12+
<li>A <b>subarray</b> is a contiguous non-empty sequence of elements within an array.</li>
13+
<li>A non-empty array is <strong>semi-decreasing</strong> if its first element is <strong>strictly greater</strong> than its last element.</li>
14+
</ul>
15+
16+
<p>&nbsp;</p>
17+
<p><strong class="example">Example 1:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> nums = [7,6,5,4,3,2,1,6,10,11]
21+
<strong>Output:</strong> 8
22+
<strong>Explanation:</strong> Take the subarray [7,6,5,4,3,2,1,6].
23+
The first element is 7 and the last one is 6 so the condition is met.
24+
Hence, the answer would be the length of the subarray or 8.
25+
It can be shown that there aren&#39;t any subarrays with the given condition with a length greater than 8.
26+
</pre>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> nums = [57,55,50,60,61,58,63,59,64,60,63]
32+
<strong>Output:</strong> 6
33+
<strong>Explanation:</strong> Take the subarray [61,58,63,59,64,60].
34+
The first element is 61 and the last one is 60 so the condition is met.
35+
Hence, the answer would be the length of the subarray or 6.
36+
It can be shown that there aren&#39;t any subarrays with the given condition with a length greater than 6.
37+
</pre>
38+
39+
<p><strong class="example">Example 3:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong> nums = [1,2,3,4]
43+
<strong>Output:</strong> 0
44+
<strong>Explanation:</strong> Since there are no semi-decreasing subarrays in the given array, the answer is 0.
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
49+
50+
<ul>
51+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
52+
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
53+
</ul>
54+
55+
## Solutions
56+
57+
<!-- tabs:start -->
58+
59+
### **Python3**
60+
61+
```python
62+
class Solution:
63+
def maxSubarrayLength(self, nums: List[int]) -> int:
64+
d = defaultdict(list)
65+
for i, x in enumerate(nums):
66+
d[x].append(i)
67+
ans, k = 0, inf
68+
for x in sorted(d, reverse=True):
69+
ans = max(ans, d[x][-1] - k + 1)
70+
k = min(k, d[x][0])
71+
return ans
72+
```
73+
74+
### **Java**
75+
76+
```java
77+
class Solution {
78+
public int maxSubarrayLength(int[] nums) {
79+
TreeMap<Integer, List<Integer>> d = new TreeMap<>(Comparator.reverseOrder());
80+
for (int i = 0; i < nums.length; ++i) {
81+
d.computeIfAbsent(nums[i], k -> new ArrayList<>()).add(i);
82+
}
83+
int ans = 0, k = 1 << 30;
84+
for (List<Integer> idx : d.values()) {
85+
ans = Math.max(ans, idx.get(idx.size() - 1) - k + 1);
86+
k = Math.min(k, idx.get(0));
87+
}
88+
return ans;
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
int maxSubarrayLength(vector<int>& nums) {
99+
map<int, vector<int>, greater<int>> d;
100+
for (int i = 0; i < nums.size(); ++i) {
101+
d[nums[i]].push_back(i);
102+
}
103+
int ans = 0, k = 1 << 30;
104+
for (auto& [_, idx] : d) {
105+
ans = max(ans, idx.back() - k + 1);
106+
k = min(k, idx[0]);
107+
}
108+
return ans;
109+
}
110+
};
111+
```
112+
113+
### **Go**
114+
115+
```go
116+
func maxSubarrayLength(nums []int) (ans int) {
117+
d := map[int][]int{}
118+
for i, x := range nums {
119+
d[x] = append(d[x], i)
120+
}
121+
keys := []int{}
122+
for x := range d {
123+
keys = append(keys, x)
124+
}
125+
sort.Slice(keys, func(i, j int) bool { return keys[i] > keys[j] })
126+
k := 1 << 30
127+
for _, x := range keys {
128+
idx := d[x]
129+
ans = max(ans, idx[len(idx)-1]-k+1)
130+
k = min(k, idx[0])
131+
}
132+
return ans
133+
}
134+
135+
func min(a, b int) int {
136+
if a < b {
137+
return a
138+
}
139+
return b
140+
}
141+
142+
func max(a, b int) int {
143+
if a > b {
144+
return a
145+
}
146+
return b
147+
}
148+
```
149+
150+
### **TypeScript**
151+
152+
```ts
153+
function maxSubarrayLength(nums: number[]): number {
154+
const d: Map<number, number[]> = new Map();
155+
for (let i = 0; i < nums.length; ++i) {
156+
if (!d.has(nums[i])) {
157+
d.set(nums[i], []);
158+
}
159+
d.get(nums[i])!.push(i);
160+
}
161+
const keys = Array.from(d.keys()).sort((a, b) => b - a);
162+
let ans = 0;
163+
let k = Infinity;
164+
for (const x of keys) {
165+
const idx = d.get(x)!;
166+
ans = Math.max(ans, idx.at(-1) - k + 1);
167+
k = Math.min(k, idx[0]);
168+
}
169+
return ans;
170+
}
171+
```
172+
173+
### **...**
174+
175+
```
176+
177+
```
178+
179+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int maxSubarrayLength(vector<int>& nums) {
4+
map<int, vector<int>, greater<int>> d;
5+
for (int i = 0; i < nums.size(); ++i) {
6+
d[nums[i]].push_back(i);
7+
}
8+
int ans = 0, k = 1 << 30;
9+
for (auto& [_, idx] : d) {
10+
ans = max(ans, idx.back() - k + 1);
11+
k = min(k, idx[0]);
12+
}
13+
return ans;
14+
}
15+
};

0 commit comments

Comments
 (0)