Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 06894ad

Browse files
authored
feat: add solutions to lc problem: No.0862 (doocs#3770)
1 parent 09083f5 commit 06894ad

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed

‎solution/0800-0899/0862.Shortest Subarray with Sum at Least K/README.md‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,64 @@ func shortestSubarray(nums []int, k int) int {
185185
}
186186
```
187187

188+
#### TypeScript
189+
190+
```ts
191+
function shortestSubarray(nums: number[], k: number): number {
192+
const [n, MAX] = [nums.length, Number.POSITIVE_INFINITY];
193+
const s = Array(n + 1).fill(0);
194+
const q: number[] = [];
195+
let ans = MAX;
196+
197+
for (let i = 0; i < n; i++) {
198+
s[i + 1] = s[i] + nums[i];
199+
}
200+
201+
for (let i = 0; i < n + 1; i++) {
202+
while (q.length && s[i] - s[q[0]] >= k) {
203+
ans = Math.min(ans, i - q.shift()!);
204+
}
205+
206+
while (q.length && s[i] <= s[q.at(-1)!]) {
207+
q.pop();
208+
}
209+
210+
q.push(i);
211+
}
212+
213+
return ans === MAX ? -1 : ans;
214+
}
215+
```
216+
217+
#### JavaScript
218+
219+
```js
220+
function shortestSubarray(nums, k) {
221+
const [n, MAX] = [nums.length, Number.POSITIVE_INFINITY];
222+
const s = Array(n + 1).fill(0);
223+
const q = [];
224+
let ans = MAX;
225+
226+
for (let i = 0; i < n; i++) {
227+
s[i + 1] = s[i] + nums[i];
228+
}
229+
230+
for (let i = 0; i < n + 1; i++) {
231+
while (q.length && s[i] - s[q[0]] >= k) {
232+
ans = Math.min(ans, i - q.shift());
233+
}
234+
235+
while (q.length && s[i] <= s[q.at(-1)]) {
236+
q.pop();
237+
}
238+
239+
q.push(i);
240+
}
241+
242+
return ans === MAX ? -1 : ans;
243+
}
244+
```
245+
188246
<!-- tabs:end -->
189247

190248
<!-- solution:end -->

‎solution/0800-0899/0862.Shortest Subarray with Sum at Least K/README_EN.md‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,64 @@ func shortestSubarray(nums []int, k int) int {
151151
}
152152
```
153153

154+
#### TypeScript
155+
156+
```ts
157+
function shortestSubarray(nums: number[], k: number): number {
158+
const [n, MAX] = [nums.length, Number.POSITIVE_INFINITY];
159+
const s = Array(n + 1).fill(0);
160+
const q: number[] = [];
161+
let ans = MAX;
162+
163+
for (let i = 0; i < n; i++) {
164+
s[i + 1] = s[i] + nums[i];
165+
}
166+
167+
for (let i = 0; i < n + 1; i++) {
168+
while (q.length && s[i] - s[q[0]] >= k) {
169+
ans = Math.min(ans, i - q.shift()!);
170+
}
171+
172+
while (q.length && s[i] <= s[q.at(-1)!]) {
173+
q.pop();
174+
}
175+
176+
q.push(i);
177+
}
178+
179+
return ans === MAX ? -1 : ans;
180+
}
181+
```
182+
183+
#### JavaScript
184+
185+
```js
186+
function shortestSubarray(nums, k) {
187+
const [n, MAX] = [nums.length, Number.POSITIVE_INFINITY];
188+
const s = Array(n + 1).fill(0);
189+
const q = [];
190+
let ans = MAX;
191+
192+
for (let i = 0; i < n; i++) {
193+
s[i + 1] = s[i] + nums[i];
194+
}
195+
196+
for (let i = 0; i < n + 1; i++) {
197+
while (q.length && s[i] - s[q[0]] >= k) {
198+
ans = Math.min(ans, i - q.shift());
199+
}
200+
201+
while (q.length && s[i] <= s[q.at(-1)]) {
202+
q.pop();
203+
}
204+
205+
q.push(i);
206+
}
207+
208+
return ans === MAX ? -1 : ans;
209+
}
210+
```
211+
154212
<!-- tabs:end -->
155213

156214
<!-- solution:end -->
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function shortestSubarray(nums, k) {
2+
const [n, MAX] = [nums.length, Number.POSITIVE_INFINITY];
3+
const s = Array(n + 1).fill(0);
4+
const q = [];
5+
let ans = MAX;
6+
7+
for (let i = 0; i < n; i++) {
8+
s[i + 1] = s[i] + nums[i];
9+
}
10+
11+
for (let i = 0; i < n + 1; i++) {
12+
while (q.length && s[i] - s[q[0]] >= k) {
13+
ans = Math.min(ans, i - q.shift());
14+
}
15+
16+
while (q.length && s[i] <= s[q.at(-1)]) {
17+
q.pop();
18+
}
19+
20+
q.push(i);
21+
}
22+
23+
return ans === MAX ? -1 : ans;
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function shortestSubarray(nums: number[], k: number): number {
2+
const [n, MAX] = [nums.length, Number.POSITIVE_INFINITY];
3+
const s = Array(n + 1).fill(0);
4+
const q: number[] = [];
5+
let ans = MAX;
6+
7+
for (let i = 0; i < n; i++) {
8+
s[i + 1] = s[i] + nums[i];
9+
}
10+
11+
for (let i = 0; i < n + 1; i++) {
12+
while (q.length && s[i] - s[q[0]] >= k) {
13+
ans = Math.min(ans, i - q.shift()!);
14+
}
15+
16+
while (q.length && s[i] <= s[q.at(-1)!]) {
17+
q.pop();
18+
}
19+
20+
q.push(i);
21+
}
22+
23+
return ans === MAX ? -1 : ans;
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /