Skip to content

Commit 40e6e43

Browse files
authored
feat: add solutions for lc No.3835,3836 (#5022)
1 parent ad3e569 commit 40e6e43

File tree

14 files changed

+933
-16
lines changed

14 files changed

+933
-16
lines changed

solution/3800-3899/3835.Count Subarrays With Cost Less Than or Equal to K/README.md

Lines changed: 163 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,191 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3835.Co
9494

9595
<!-- solution:start -->
9696

97-
### 方法一
97+
### 方法一:双端队列 + 枚举 + 双指针
98+
99+
我们注意到,如果一个子数组 $\text{nums}[l..r]$ 的开销小于或等于 $k$,则对于任意 $l' \geq l$ 和 $r' \leq r$,子数组 $\text{nums}[l'..r']$ 的开销也小于或等于 $k$。因此,我们可以枚举右端点 $r$,使用双指针维护满足条件的最小左端点 $l$,那么以 $r$ 结尾的满足条件的子数组数量为 $r - l + 1$,我们将其累加到答案中即可。
100+
101+
我们可以使用两个双端队列分别维护当前窗口内的最大值和最小值。
102+
103+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\text{nums}$ 的长度。
98104

99105
<!-- tabs:start -->
100106

101107
#### Python3
102108

103109
```python
104-
110+
class Solution:
111+
def countSubarrays(self, nums: List[int], k: int) -> int:
112+
ans = 0
113+
q1 = deque()
114+
q2 = deque()
115+
l = 0
116+
for r, x in enumerate(nums):
117+
while q1 and nums[q1[-1]] <= x:
118+
q1.pop()
119+
while q2 and nums[q2[-1]] >= x:
120+
q2.pop()
121+
q1.append(r)
122+
q2.append(r)
123+
while l < r and (nums[q1[0]] - nums[q2[0]]) * (r - l + 1) > k:
124+
l += 1
125+
if q1[0] < l:
126+
q1.popleft()
127+
if q2[0] < l:
128+
q2.popleft()
129+
ans += r - l + 1
130+
return ans
105131
```
106132

107133
#### Java
108134

109135
```java
110-
136+
class Solution {
137+
public long countSubarrays(int[] nums, long k) {
138+
long ans = 0;
139+
Deque<Integer> q1 = new ArrayDeque<>();
140+
Deque<Integer> q2 = new ArrayDeque<>();
141+
int l = 0;
142+
143+
for (int r = 0; r < nums.length; r++) {
144+
int x = nums[r];
145+
146+
while (!q1.isEmpty() && nums[q1.peekLast()] <= x) {
147+
q1.pollLast();
148+
}
149+
while (!q2.isEmpty() && nums[q2.peekLast()] >= x) {
150+
q2.pollLast();
151+
}
152+
q1.addLast(r);
153+
q2.addLast(r);
154+
155+
while (
156+
l < r && (long) (nums[q1.peekFirst()] - nums[q2.peekFirst()]) * (r - l + 1) > k) {
157+
l++;
158+
if (q1.peekFirst() < l) {
159+
q1.pollFirst();
160+
}
161+
if (q2.peekFirst() < l) {
162+
q2.pollFirst();
163+
}
164+
}
165+
166+
ans += r - l + 1;
167+
}
168+
return ans;
169+
}
170+
}
111171
```
112172

113173
#### C++
114174

115175
```cpp
116-
176+
class Solution {
177+
public:
178+
long long countSubarrays(vector<int>& nums, long long k) {
179+
long long ans = 0;
180+
deque<int> q1, q2;
181+
int l = 0;
182+
183+
for (int r = 0; r < nums.size(); r++) {
184+
int x = nums[r];
185+
186+
while (!q1.empty() && nums[q1.back()] <= x) {
187+
q1.pop_back();
188+
}
189+
while (!q2.empty() && nums[q2.back()] >= x) {
190+
q2.pop_back();
191+
}
192+
q1.push_back(r);
193+
q2.push_back(r);
194+
195+
while (l < r && (long long) (nums[q1.front()] - nums[q2.front()]) * (r - l + 1) > k) {
196+
l++;
197+
if (q1.front() < l) {
198+
q1.pop_front();
199+
}
200+
if (q2.front() < l) {
201+
q2.pop_front();
202+
}
203+
}
204+
205+
ans += r - l + 1;
206+
}
207+
return ans;
208+
}
209+
};
117210
```
118211

119212
#### Go
120213

121214
```go
215+
func countSubarrays(nums []int, k int64) int64 {
216+
var ans int64 = 0
217+
q1 := make([]int, 0)
218+
q2 := make([]int, 0)
219+
l := 0
220+
221+
for r, x := range nums {
222+
for len(q1) > 0 && nums[q1[len(q1)-1]] <= x {
223+
q1 = q1[:len(q1)-1]
224+
}
225+
for len(q2) > 0 && nums[q2[len(q2)-1]] >= x {
226+
q2 = q2[:len(q2)-1]
227+
}
228+
q1 = append(q1, r)
229+
q2 = append(q2, r)
230+
231+
for l < r &&
232+
int64(nums[q1[0]]-nums[q2[0]])*int64(r-l+1) > k {
233+
l++
234+
if q1[0] < l {
235+
q1 = q1[1:]
236+
}
237+
if q2[0] < l {
238+
q2 = q2[1:]
239+
}
240+
}
241+
ans += int64(r - l + 1)
242+
}
243+
return ans
244+
}
245+
```
122246

247+
#### TypeScript
248+
249+
```ts
250+
function countSubarrays(nums: number[], k: number): number {
251+
let ans = 0;
252+
const q1: number[] = [];
253+
const q2: number[] = [];
254+
let h1 = 0,
255+
t1 = 0;
256+
let h2 = 0,
257+
t2 = 0;
258+
let l = 0;
259+
for (let r = 0; r < nums.length; r++) {
260+
const x = nums[r];
261+
while (h1 < t1 && nums[q1[t1 - 1]] <= x) {
262+
t1--;
263+
}
264+
while (h2 < t2 && nums[q2[t2 - 1]] >= x) {
265+
t2--;
266+
}
267+
q1[t1++] = r;
268+
q2[t2++] = r;
269+
while (l < r && (nums[q1[h1]] - nums[q2[h2]]) * (r - l + 1) > k) {
270+
l++;
271+
if (q1[h1] < l) {
272+
h1++;
273+
}
274+
if (q2[h2] < l) {
275+
h2++;
276+
}
277+
}
278+
ans += r - l + 1;
279+
}
280+
return ans;
281+
}
123282
```
124283

125284
<!-- tabs:end -->

solution/3800-3899/3835.Count Subarrays With Cost Less Than or Equal to K/README_EN.md

Lines changed: 163 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,191 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3835.Co
8989

9090
<!-- solution:start -->
9191

92-
### Solution 1
92+
### Solution 1: Deque + Enumeration + Two Pointers
93+
94+
We notice that if a subarray $\text{nums}[l..r]$ has a cost less than or equal to $k$, then for any $l' \geq l$ and $r' \leq r$, the subarray $\text{nums}[l'..r']$ also has a cost less than or equal to $k$. Therefore, we can enumerate the right endpoint $r$, use two pointers to maintain the minimum left endpoint $l$ that satisfies the condition, then the number of subarrays ending at $r$ that satisfy the condition is $r - l + 1$, which we accumulate to the answer.
95+
96+
We can use two deques to maintain the maximum and minimum values in the current window respectively.
97+
98+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $\text{nums}$.
9399

94100
<!-- tabs:start -->
95101

96102
#### Python3
97103

98104
```python
99-
105+
class Solution:
106+
def countSubarrays(self, nums: List[int], k: int) -> int:
107+
ans = 0
108+
q1 = deque()
109+
q2 = deque()
110+
l = 0
111+
for r, x in enumerate(nums):
112+
while q1 and nums[q1[-1]] <= x:
113+
q1.pop()
114+
while q2 and nums[q2[-1]] >= x:
115+
q2.pop()
116+
q1.append(r)
117+
q2.append(r)
118+
while l < r and (nums[q1[0]] - nums[q2[0]]) * (r - l + 1) > k:
119+
l += 1
120+
if q1[0] < l:
121+
q1.popleft()
122+
if q2[0] < l:
123+
q2.popleft()
124+
ans += r - l + 1
125+
return ans
100126
```
101127

102128
#### Java
103129

104130
```java
105-
131+
class Solution {
132+
public long countSubarrays(int[] nums, long k) {
133+
long ans = 0;
134+
Deque<Integer> q1 = new ArrayDeque<>();
135+
Deque<Integer> q2 = new ArrayDeque<>();
136+
int l = 0;
137+
138+
for (int r = 0; r < nums.length; r++) {
139+
int x = nums[r];
140+
141+
while (!q1.isEmpty() && nums[q1.peekLast()] <= x) {
142+
q1.pollLast();
143+
}
144+
while (!q2.isEmpty() && nums[q2.peekLast()] >= x) {
145+
q2.pollLast();
146+
}
147+
q1.addLast(r);
148+
q2.addLast(r);
149+
150+
while (
151+
l < r && (long) (nums[q1.peekFirst()] - nums[q2.peekFirst()]) * (r - l + 1) > k) {
152+
l++;
153+
if (q1.peekFirst() < l) {
154+
q1.pollFirst();
155+
}
156+
if (q2.peekFirst() < l) {
157+
q2.pollFirst();
158+
}
159+
}
160+
161+
ans += r - l + 1;
162+
}
163+
return ans;
164+
}
165+
}
106166
```
107167

108168
#### C++
109169

110170
```cpp
111-
171+
class Solution {
172+
public:
173+
long long countSubarrays(vector<int>& nums, long long k) {
174+
long long ans = 0;
175+
deque<int> q1, q2;
176+
int l = 0;
177+
178+
for (int r = 0; r < nums.size(); r++) {
179+
int x = nums[r];
180+
181+
while (!q1.empty() && nums[q1.back()] <= x) {
182+
q1.pop_back();
183+
}
184+
while (!q2.empty() && nums[q2.back()] >= x) {
185+
q2.pop_back();
186+
}
187+
q1.push_back(r);
188+
q2.push_back(r);
189+
190+
while (l < r && (long long) (nums[q1.front()] - nums[q2.front()]) * (r - l + 1) > k) {
191+
l++;
192+
if (q1.front() < l) {
193+
q1.pop_front();
194+
}
195+
if (q2.front() < l) {
196+
q2.pop_front();
197+
}
198+
}
199+
200+
ans += r - l + 1;
201+
}
202+
return ans;
203+
}
204+
};
112205
```
113206

114207
#### Go
115208

116209
```go
210+
func countSubarrays(nums []int, k int64) int64 {
211+
var ans int64 = 0
212+
q1 := make([]int, 0)
213+
q2 := make([]int, 0)
214+
l := 0
215+
216+
for r, x := range nums {
217+
for len(q1) > 0 && nums[q1[len(q1)-1]] <= x {
218+
q1 = q1[:len(q1)-1]
219+
}
220+
for len(q2) > 0 && nums[q2[len(q2)-1]] >= x {
221+
q2 = q2[:len(q2)-1]
222+
}
223+
q1 = append(q1, r)
224+
q2 = append(q2, r)
225+
226+
for l < r &&
227+
int64(nums[q1[0]]-nums[q2[0]])*int64(r-l+1) > k {
228+
l++
229+
if q1[0] < l {
230+
q1 = q1[1:]
231+
}
232+
if q2[0] < l {
233+
q2 = q2[1:]
234+
}
235+
}
236+
ans += int64(r - l + 1)
237+
}
238+
return ans
239+
}
240+
```
117241

242+
#### TypeScript
243+
244+
```ts
245+
function countSubarrays(nums: number[], k: number): number {
246+
let ans = 0;
247+
const q1: number[] = [];
248+
const q2: number[] = [];
249+
let h1 = 0,
250+
t1 = 0;
251+
let h2 = 0,
252+
t2 = 0;
253+
let l = 0;
254+
for (let r = 0; r < nums.length; r++) {
255+
const x = nums[r];
256+
while (h1 < t1 && nums[q1[t1 - 1]] <= x) {
257+
t1--;
258+
}
259+
while (h2 < t2 && nums[q2[t2 - 1]] >= x) {
260+
t2--;
261+
}
262+
q1[t1++] = r;
263+
q2[t2++] = r;
264+
while (l < r && (nums[q1[h1]] - nums[q2[h2]]) * (r - l + 1) > k) {
265+
l++;
266+
if (q1[h1] < l) {
267+
h1++;
268+
}
269+
if (q2[h2] < l) {
270+
h2++;
271+
}
272+
}
273+
ans += r - l + 1;
274+
}
275+
return ans;
276+
}
118277
```
119278

120279
<!-- tabs:end -->

0 commit comments

Comments
 (0)