Skip to content

Commit cf4325d

Browse files
committed
feat: add solutions to lc problem: No.1438
No.1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
1 parent 6956ea3 commit cf4325d

File tree

6 files changed

+267
-2
lines changed

6 files changed

+267
-2
lines changed

solution/1400-1499/1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/README.md

+96-1
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,117 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60+
**方法一:有序集合 + 滑动窗口**
61+
62+
我们可以枚举每个位置作为子数组的右端点,找到其对应的最靠左的左端点,满足区间内中最大值与最小值的差值不超过 $limit$。过程中,我们用有序集合维护窗口内的最大值和最小值。
63+
64+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `nums` 的长度。
65+
6066
<!-- tabs:start -->
6167

6268
### **Python3**
6369

6470
<!-- 这里可写当前语言的特殊实现逻辑 -->
6571

6672
```python
67-
73+
from sortedcontainers import SortedList
74+
75+
76+
class Solution:
77+
def longestSubarray(self, nums: List[int], limit: int) -> int:
78+
sl = SortedList()
79+
ans = j = 0
80+
for i, v in enumerate(nums):
81+
sl.add(v)
82+
while sl[-1] - sl[0] > limit:
83+
sl.remove(nums[j])
84+
j += 1
85+
ans = max(ans, i - j + 1)
86+
return ans
6887
```
6988

7089
### **Java**
7190

7291
<!-- 这里可写当前语言的特殊实现逻辑 -->
7392

7493
```java
94+
class Solution {
95+
public int longestSubarray(int[] nums, int limit) {
96+
TreeMap<Integer, Integer> tm = new TreeMap<>();
97+
int ans = 0, j = 0;
98+
for (int i = 0; i < nums.length; ++i) {
99+
tm.put(nums[i], tm.getOrDefault(nums[i], 0) + 1);
100+
while (tm.lastKey() - tm.firstKey() > limit) {
101+
tm.put(nums[j], tm.get(nums[j]) - 1);
102+
if (tm.get(nums[j]) == 0) {
103+
tm.remove(nums[j]);
104+
}
105+
++j;
106+
}
107+
ans = Math.max(ans, i - j + 1);
108+
}
109+
return ans;
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
int longestSubarray(vector<int>& nums, int limit) {
120+
multiset<int> s;
121+
int ans = 0, j = 0;
122+
for (int i = 0; i < nums.size(); ++i) {
123+
s.insert(nums[i]);
124+
while (*s.rbegin() - *s.begin() > limit) {
125+
s.erase(s.find(nums[j++]));
126+
}
127+
ans = max(ans, i - j + 1);
128+
}
129+
return ans;
130+
}
131+
};
132+
```
75133
134+
### **Go**
135+
136+
```go
137+
func longestSubarray(nums []int, limit int) (ans int) {
138+
tm := treemap.NewWithIntComparator()
139+
j := 0
140+
for i, v := range nums {
141+
if x, ok := tm.Get(v); ok {
142+
tm.Put(v, x.(int)+1)
143+
} else {
144+
tm.Put(v, 1)
145+
}
146+
for {
147+
a, _ := tm.Min()
148+
b, _ := tm.Max()
149+
if b.(int)-a.(int) > limit {
150+
if x, _ := tm.Get(nums[j]); x.(int) == 1 {
151+
tm.Remove(nums[j])
152+
} else {
153+
tm.Put(nums[j], x.(int)-1)
154+
}
155+
j++
156+
} else {
157+
break
158+
}
159+
}
160+
ans = max(ans, i-j+1)
161+
}
162+
return
163+
}
164+
165+
func max(a, b int) int {
166+
if a > b {
167+
return a
168+
}
169+
return b
170+
}
76171
```
77172

78173
### **...**

solution/1400-1499/1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/README_EN.md

+90-1
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,102 @@ Therefore, the size of the longest subarray is 2.
5757
### **Python3**
5858

5959
```python
60-
60+
from sortedcontainers import SortedList
61+
62+
63+
class Solution:
64+
def longestSubarray(self, nums: List[int], limit: int) -> int:
65+
sl = SortedList()
66+
ans = j = 0
67+
for i, v in enumerate(nums):
68+
sl.add(v)
69+
while sl[-1] - sl[0] > limit:
70+
sl.remove(nums[j])
71+
j += 1
72+
ans = max(ans, i - j + 1)
73+
return ans
6174
```
6275

6376
### **Java**
6477

6578
```java
79+
class Solution {
80+
public int longestSubarray(int[] nums, int limit) {
81+
TreeMap<Integer, Integer> tm = new TreeMap<>();
82+
int ans = 0, j = 0;
83+
for (int i = 0; i < nums.length; ++i) {
84+
tm.put(nums[i], tm.getOrDefault(nums[i], 0) + 1);
85+
while (tm.lastKey() - tm.firstKey() > limit) {
86+
tm.put(nums[j], tm.get(nums[j]) - 1);
87+
if (tm.get(nums[j]) == 0) {
88+
tm.remove(nums[j]);
89+
}
90+
++j;
91+
}
92+
ans = Math.max(ans, i - j + 1);
93+
}
94+
return ans;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int longestSubarray(vector<int>& nums, int limit) {
105+
multiset<int> s;
106+
int ans = 0, j = 0;
107+
for (int i = 0; i < nums.size(); ++i) {
108+
s.insert(nums[i]);
109+
while (*s.rbegin() - *s.begin() > limit) {
110+
s.erase(s.find(nums[j++]));
111+
}
112+
ans = max(ans, i - j + 1);
113+
}
114+
return ans;
115+
}
116+
};
117+
```
66118
119+
### **Go**
120+
121+
```go
122+
func longestSubarray(nums []int, limit int) (ans int) {
123+
tm := treemap.NewWithIntComparator()
124+
j := 0
125+
for i, v := range nums {
126+
if x, ok := tm.Get(v); ok {
127+
tm.Put(v, x.(int)+1)
128+
} else {
129+
tm.Put(v, 1)
130+
}
131+
for {
132+
a, _ := tm.Min()
133+
b, _ := tm.Max()
134+
if b.(int)-a.(int) > limit {
135+
if x, _ := tm.Get(nums[j]); x.(int) == 1 {
136+
tm.Remove(nums[j])
137+
} else {
138+
tm.Put(nums[j], x.(int)-1)
139+
}
140+
j++
141+
} else {
142+
break
143+
}
144+
}
145+
ans = max(ans, i-j+1)
146+
}
147+
return
148+
}
149+
150+
func max(a, b int) int {
151+
if a > b {
152+
return a
153+
}
154+
return b
155+
}
67156
```
68157

69158
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int longestSubarray(vector<int>& nums, int limit) {
4+
multiset<int> s;
5+
int ans = 0, j = 0;
6+
for (int i = 0; i < nums.size(); ++i) {
7+
s.insert(nums[i]);
8+
while (*s.rbegin() - *s.begin() > limit) {
9+
s.erase(s.find(nums[j++]));
10+
}
11+
ans = max(ans, i - j + 1);
12+
}
13+
return ans;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
func longestSubarray(nums []int, limit int) (ans int) {
2+
tm := treemap.NewWithIntComparator()
3+
j := 0
4+
for i, v := range nums {
5+
if x, ok := tm.Get(v); ok {
6+
tm.Put(v, x.(int)+1)
7+
} else {
8+
tm.Put(v, 1)
9+
}
10+
for {
11+
a, _ := tm.Min()
12+
b, _ := tm.Max()
13+
if b.(int)-a.(int) > limit {
14+
if x, _ := tm.Get(nums[j]); x.(int) == 1 {
15+
tm.Remove(nums[j])
16+
} else {
17+
tm.Put(nums[j], x.(int)-1)
18+
}
19+
j++
20+
} else {
21+
break
22+
}
23+
}
24+
ans = max(ans, i-j+1)
25+
}
26+
return
27+
}
28+
29+
func max(a, b int) int {
30+
if a > b {
31+
return a
32+
}
33+
return b
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int longestSubarray(int[] nums, int limit) {
3+
TreeMap<Integer, Integer> tm = new TreeMap<>();
4+
int ans = 0, j = 0;
5+
for (int i = 0; i < nums.length; ++i) {
6+
tm.put(nums[i], tm.getOrDefault(nums[i], 0) + 1);
7+
while (tm.lastKey() - tm.firstKey() > limit) {
8+
tm.put(nums[j], tm.get(nums[j]) - 1);
9+
if (tm.get(nums[j]) == 0) {
10+
tm.remove(nums[j]);
11+
}
12+
++j;
13+
}
14+
ans = Math.max(ans, i - j + 1);
15+
}
16+
return ans;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from sortedcontainers import SortedList
2+
3+
4+
class Solution:
5+
def longestSubarray(self, nums: List[int], limit: int) -> int:
6+
sl = SortedList()
7+
ans = j = 0
8+
for i, v in enumerate(nums):
9+
sl.add(v)
10+
while sl[-1] - sl[0] > limit:
11+
sl.remove(nums[j])
12+
j += 1
13+
ans = max(ans, i - j + 1)
14+
return ans

0 commit comments

Comments
 (0)