diff --git a/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/README.md b/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/README.md index a00bc5e41f383..50f971e87bf57 100644 --- a/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/README.md +++ b/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/README.md @@ -185,6 +185,64 @@ func shortestSubarray(nums []int, k int) int { } ``` +#### TypeScript + +```ts +function shortestSubarray(nums: number[], k: number): number { + const [n, MAX] = [nums.length, Number.POSITIVE_INFINITY]; + const s = Array(n + 1).fill(0); + const q: number[] = []; + let ans = MAX; + + for (let i = 0; i < n; i++) { + s[i + 1] = s[i] + nums[i]; + } + + for (let i = 0; i < n + 1; i++) { + while (q.length && s[i] - s[q[0]] >= k) { + ans = Math.min(ans, i - q.shift()!); + } + + while (q.length && s[i] <= s[q.at(-1)!]) { + q.pop(); + } + + q.push(i); + } + + return ans === MAX ? -1 : ans; +} +``` + +#### JavaScript + +```js +function shortestSubarray(nums, k) { + const [n, MAX] = [nums.length, Number.POSITIVE_INFINITY]; + const s = Array(n + 1).fill(0); + const q = []; + let ans = MAX; + + for (let i = 0; i < n; i++) { + s[i + 1] = s[i] + nums[i]; + } + + for (let i = 0; i < n + 1; i++) { + while (q.length && s[i] - s[q[0]] >= k) { + ans = Math.min(ans, i - q.shift()); + } + + while (q.length && s[i] <= s[q.at(-1)]) { + q.pop(); + } + + q.push(i); + } + + return ans === MAX ? -1 : ans; +} +``` + diff --git a/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/README_EN.md b/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/README_EN.md index ca2946575c688..e74a48903192e 100644 --- a/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/README_EN.md +++ b/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/README_EN.md @@ -151,6 +151,64 @@ func shortestSubarray(nums []int, k int) int { } ``` +#### TypeScript + +```ts +function shortestSubarray(nums: number[], k: number): number { + const [n, MAX] = [nums.length, Number.POSITIVE_INFINITY]; + const s = Array(n + 1).fill(0); + const q: number[] = []; + let ans = MAX; + + for (let i = 0; i < n; i++) { + s[i + 1] = s[i] + nums[i]; + } + + for (let i = 0; i < n + 1; i++) { + while (q.length && s[i] - s[q[0]] >= k) { + ans = Math.min(ans, i - q.shift()!); + } + + while (q.length && s[i] <= s[q.at(-1)!]) { + q.pop(); + } + + q.push(i); + } + + return ans === MAX ? -1 : ans; +} +``` + +#### JavaScript + +```js +function shortestSubarray(nums, k) { + const [n, MAX] = [nums.length, Number.POSITIVE_INFINITY]; + const s = Array(n + 1).fill(0); + const q = []; + let ans = MAX; + + for (let i = 0; i < n; i++) { + s[i + 1] = s[i] + nums[i]; + } + + for (let i = 0; i < n + 1; i++) { + while (q.length && s[i] - s[q[0]] >= k) { + ans = Math.min(ans, i - q.shift()); + } + + while (q.length && s[i] <= s[q.at(-1)]) { + q.pop(); + } + + q.push(i); + } + + return ans === MAX ? -1 : ans; +} +``` + diff --git a/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/Solution.js b/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/Solution.js new file mode 100644 index 0000000000000..6d165225adbbe --- /dev/null +++ b/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/Solution.js @@ -0,0 +1,24 @@ +function shortestSubarray(nums, k) { + const [n, MAX] = [nums.length, Number.POSITIVE_INFINITY]; + const s = Array(n + 1).fill(0); + const q = []; + let ans = MAX; + + for (let i = 0; i < n; i++) { + s[i + 1] = s[i] + nums[i]; + } + + for (let i = 0; i < n + 1; i++) { + while (q.length && s[i] - s[q[0]] >= k) { + ans = Math.min(ans, i - q.shift()); + } + + while (q.length && s[i] <= s[q.at(-1)]) { + q.pop(); + } + + q.push(i); + } + + return ans === MAX ? -1 : ans; +} diff --git a/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/Solution.ts b/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/Solution.ts new file mode 100644 index 0000000000000..92ffc6fcaff03 --- /dev/null +++ b/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/Solution.ts @@ -0,0 +1,24 @@ +function shortestSubarray(nums: number[], k: number): number { + const [n, MAX] = [nums.length, Number.POSITIVE_INFINITY]; + const s = Array(n + 1).fill(0); + const q: number[] = []; + let ans = MAX; + + for (let i = 0; i < n; i++) { + s[i + 1] = s[i] + nums[i]; + } + + for (let i = 0; i < n + 1; i++) { + while (q.length && s[i] - s[q[0]] >= k) { + ans = Math.min(ans, i - q.shift()!); + } + + while (q.length && s[i] <= s[q.at(-1)!]) { + q.pop(); + } + + q.push(i); + } + + return ans === MAX ? -1 : ans; +}