From 6a538b20791950c6c63b0f6d259275ab1fa27cc3 Mon Sep 17 00:00:00 2001 From: rain84 Date: Sun, 17 Nov 2024 23:09:08 +0300 Subject: [PATCH] feat: add solutions to lc problem: No.0862 --- .../README.md | 58 +++++++++++++++++++ .../README_EN.md | 58 +++++++++++++++++++ .../Solution.js | 24 ++++++++ .../Solution.ts | 24 ++++++++ 4 files changed, 164 insertions(+) create mode 100644 solution/0800-0899/0862.Shortest Subarray with Sum at Least K/Solution.js create mode 100644 solution/0800-0899/0862.Shortest Subarray with Sum at Least K/Solution.ts 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; +}