From bc23fa6e38480b9cea0faeb5e443b93f707449fa Mon Sep 17 00:00:00 2001 From: Subhadip Hensh <91666506+07subhadip@users.noreply.github.com> Date: Sat, 25 Jan 2025 10:54:36 +0530 Subject: [PATCH 1/3] Create Solution.js --- .../Solution.js | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/Solution.js diff --git a/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/Solution.js b/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/Solution.js new file mode 100644 index 0000000000000..a9762071f6cd8 --- /dev/null +++ b/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/Solution.js @@ -0,0 +1,90 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var minMaxSubarraySum = function(nums, k) { + const computeSum = (nums, k, isMin) => { + const n = nums.length; + const prev = new Array(n).fill(-1); + const next = new Array(n).fill(n); + let stack = []; + + if (isMin) { + for (let i = 0; i < n; i++) { + while (stack.length > 0 && nums[stack[stack.length - 1]] >= nums[i]) { + stack.pop(); + } + prev[i] = stack.length > 0 ? stack[stack.length - 1] : -1; + stack.push(i); + } + stack = []; + for (let i = n - 1; i >= 0; i--) { + while (stack.length > 0 && nums[stack[stack.length - 1]] > nums[i]) { + stack.pop(); + } + next[i] = stack.length > 0 ? stack[stack.length - 1] : n; + stack.push(i); + } + } else { + for (let i = 0; i < n; i++) { + while (stack.length > 0 && nums[stack[stack.length - 1]] <= nums[i]) { + stack.pop(); + } + prev[i] = stack.length > 0 ? stack[stack.length - 1] : -1; + stack.push(i); + } + stack = []; + for (let i = n - 1; i >= 0; i--) { + while (stack.length > 0 && nums[stack[stack.length - 1]] < nums[i]) { + stack.pop(); + } + next[i] = stack.length > 0 ? stack[stack.length - 1] : n; + stack.push(i); + } + } + + let sum = 0; + for (let i = 0; i < n; i++) { + const left = prev[i]; + const right = next[i]; + const a = left + 1; + const b = i; + const c = i; + const d = right - 1; + + let s_start = Math.max(a, i - k + 1); + let s_end_candidate = d - k + 1; + let s_upper = Math.min(b, s_end_candidate); + + let sum1 = 0; + if (s_upper >= s_start) { + const num_terms = s_upper - s_start + 1; + const first = s_start; + const last = s_upper; + const sum_s = (last * (last + 1)) / 2 - ((first - 1) * first) / 2; + const sum_k = (k - i) * num_terms; + sum1 = sum_s + sum_k; + } + + let s2_start = s_upper + 1; + let s2_end = b; + s2_start = Math.max(s2_start, a); + s2_end = Math.min(s2_end, b); + + let sum2 = 0; + if (s2_start <= s2_end) { + const count = s2_end - s2_start + 1; + const term = d - i + 1; + sum2 = term * count; + } + + sum += nums[i] * (sum1 + sum2); + } + return sum; + }; + + const sumMin = computeSum(nums, k, true); + const sumMax = computeSum(nums, k, false); + return sumMin + sumMax; +}; From 78f639f95f914f52449b8cd87bce0c5b85255b02 Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Sat, 25 Jan 2025 05:26:24 +0000 Subject: [PATCH 2/3] style: format code and docs with prettier --- .../Solution.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/Solution.js b/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/Solution.js index a9762071f6cd8..84796c96e94bd 100644 --- a/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/Solution.js +++ b/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/Solution.js @@ -3,13 +3,13 @@ * @param {number} k * @return {number} */ -var minMaxSubarraySum = function(nums, k) { +var minMaxSubarraySum = function (nums, k) { const computeSum = (nums, k, isMin) => { const n = nums.length; const prev = new Array(n).fill(-1); const next = new Array(n).fill(n); let stack = []; - + if (isMin) { for (let i = 0; i < n; i++) { while (stack.length > 0 && nums[stack[stack.length - 1]] >= nums[i]) { From 9624a21ef610b9bbcee87f565db8f2e15af84a05 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 8 Apr 2025 06:21:01 +0800 Subject: [PATCH 3/3] Refactor variable names and add JavaScript example. --- .../README.md | 96 +++++++++++++++++++ .../README_EN.md | 96 +++++++++++++++++++ .../Solution.js | 87 ++++++++--------- 3 files changed, 236 insertions(+), 43 deletions(-) diff --git a/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/README.md b/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/README.md index c25572b5ade76..d60ce9f087a80 100644 --- a/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/README.md +++ b/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/README.md @@ -185,6 +185,102 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3430.Ma ``` +#### JavaScript + +```js +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var minMaxSubarraySum = function (nums, k) { + const computeSum = (nums, k, isMin) => { + const n = nums.length; + const prev = Array(n).fill(-1); + const next = Array(n).fill(n); + let stk = []; + + if (isMin) { + for (let i = 0; i < n; i++) { + while (stk.length > 0 && nums[stk[stk.length - 1]] >= nums[i]) { + stk.pop(); + } + prev[i] = stk.length > 0 ? stk[stk.length - 1] : -1; + stk.push(i); + } + stk = []; + for (let i = n - 1; i >= 0; i--) { + while (stk.length > 0 && nums[stk[stk.length - 1]] > nums[i]) { + stk.pop(); + } + next[i] = stk.length > 0 ? stk[stk.length - 1] : n; + stk.push(i); + } + } else { + for (let i = 0; i < n; i++) { + while (stk.length > 0 && nums[stk[stk.length - 1]] <= nums[i]) { + stk.pop(); + } + prev[i] = stk.length > 0 ? stk[stk.length - 1] : -1; + stk.push(i); + } + stk = []; + for (let i = n - 1; i >= 0; i--) { + while (stk.length > 0 && nums[stk[stk.length - 1]] < nums[i]) { + stk.pop(); + } + next[i] = stk.length > 0 ? stk[stk.length - 1] : n; + stk.push(i); + } + } + + let totalSum = 0; + for (let i = 0; i < n; i++) { + const left = prev[i]; + const right = next[i]; + const a = left + 1; + const b = i; + const c = i; + const d = right - 1; + + let start1 = Math.max(a, i - k + 1); + let endCandidate1 = d - k + 1; + let upper1 = Math.min(b, endCandidate1); + + let sum1 = 0; + if (upper1 >= start1) { + const termCount = upper1 - start1 + 1; + const first = start1; + const last = upper1; + const indexSum = (last * (last + 1)) / 2 - ((first - 1) * first) / 2; + const constantSum = (k - i) * termCount; + sum1 = indexSum + constantSum; + } + + let start2 = upper1 + 1; + let end2 = b; + start2 = Math.max(start2, a); + end2 = Math.min(end2, b); + + let sum2 = 0; + if (start2 <= end2) { + const count = end2 - start2 + 1; + const term = d - i + 1; + sum2 = term * count; + } + + totalSum += nums[i] * (sum1 + sum2); + } + + return totalSum; + }; + + const minSum = computeSum(nums, k, true); + const maxSum = computeSum(nums, k, false); + return minSum + maxSum; +}; +``` + diff --git a/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/README_EN.md b/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/README_EN.md index 6e6b122336094..ae1926f86e311 100644 --- a/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/README_EN.md +++ b/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/README_EN.md @@ -182,6 +182,102 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3430.Ma ``` +#### JavaScript + +```js +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var minMaxSubarraySum = function (nums, k) { + const computeSum = (nums, k, isMin) => { + const n = nums.length; + const prev = Array(n).fill(-1); + const next = Array(n).fill(n); + let stk = []; + + if (isMin) { + for (let i = 0; i < n; i++) { + while (stk.length > 0 && nums[stk[stk.length - 1]] >= nums[i]) { + stk.pop(); + } + prev[i] = stk.length > 0 ? stk[stk.length - 1] : -1; + stk.push(i); + } + stk = []; + for (let i = n - 1; i >= 0; i--) { + while (stk.length > 0 && nums[stk[stk.length - 1]] > nums[i]) { + stk.pop(); + } + next[i] = stk.length > 0 ? stk[stk.length - 1] : n; + stk.push(i); + } + } else { + for (let i = 0; i < n; i++) { + while (stk.length > 0 && nums[stk[stk.length - 1]] <= nums[i]) { + stk.pop(); + } + prev[i] = stk.length > 0 ? stk[stk.length - 1] : -1; + stk.push(i); + } + stk = []; + for (let i = n - 1; i >= 0; i--) { + while (stk.length > 0 && nums[stk[stk.length - 1]] < nums[i]) { + stk.pop(); + } + next[i] = stk.length > 0 ? stk[stk.length - 1] : n; + stk.push(i); + } + } + + let totalSum = 0; + for (let i = 0; i < n; i++) { + const left = prev[i]; + const right = next[i]; + const a = left + 1; + const b = i; + const c = i; + const d = right - 1; + + let start1 = Math.max(a, i - k + 1); + let endCandidate1 = d - k + 1; + let upper1 = Math.min(b, endCandidate1); + + let sum1 = 0; + if (upper1 >= start1) { + const termCount = upper1 - start1 + 1; + const first = start1; + const last = upper1; + const indexSum = (last * (last + 1)) / 2 - ((first - 1) * first) / 2; + const constantSum = (k - i) * termCount; + sum1 = indexSum + constantSum; + } + + let start2 = upper1 + 1; + let end2 = b; + start2 = Math.max(start2, a); + end2 = Math.min(end2, b); + + let sum2 = 0; + if (start2 <= end2) { + const count = end2 - start2 + 1; + const term = d - i + 1; + sum2 = term * count; + } + + totalSum += nums[i] * (sum1 + sum2); + } + + return totalSum; + }; + + const minSum = computeSum(nums, k, true); + const maxSum = computeSum(nums, k, false); + return minSum + maxSum; +}; +``` + diff --git a/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/Solution.js b/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/Solution.js index 84796c96e94bd..0532979324352 100644 --- a/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/Solution.js +++ b/solution/3400-3499/3430.Maximum and Minimum Sums of at Most Size K Subarrays/Solution.js @@ -6,45 +6,45 @@ var minMaxSubarraySum = function (nums, k) { const computeSum = (nums, k, isMin) => { const n = nums.length; - const prev = new Array(n).fill(-1); - const next = new Array(n).fill(n); - let stack = []; + const prev = Array(n).fill(-1); + const next = Array(n).fill(n); + let stk = []; if (isMin) { for (let i = 0; i < n; i++) { - while (stack.length > 0 && nums[stack[stack.length - 1]] >= nums[i]) { - stack.pop(); + while (stk.length > 0 && nums[stk[stk.length - 1]] >= nums[i]) { + stk.pop(); } - prev[i] = stack.length > 0 ? stack[stack.length - 1] : -1; - stack.push(i); + prev[i] = stk.length > 0 ? stk[stk.length - 1] : -1; + stk.push(i); } - stack = []; + stk = []; for (let i = n - 1; i >= 0; i--) { - while (stack.length > 0 && nums[stack[stack.length - 1]] > nums[i]) { - stack.pop(); + while (stk.length > 0 && nums[stk[stk.length - 1]] > nums[i]) { + stk.pop(); } - next[i] = stack.length > 0 ? stack[stack.length - 1] : n; - stack.push(i); + next[i] = stk.length > 0 ? stk[stk.length - 1] : n; + stk.push(i); } } else { for (let i = 0; i < n; i++) { - while (stack.length > 0 && nums[stack[stack.length - 1]] <= nums[i]) { - stack.pop(); + while (stk.length > 0 && nums[stk[stk.length - 1]] <= nums[i]) { + stk.pop(); } - prev[i] = stack.length > 0 ? stack[stack.length - 1] : -1; - stack.push(i); + prev[i] = stk.length > 0 ? stk[stk.length - 1] : -1; + stk.push(i); } - stack = []; + stk = []; for (let i = n - 1; i >= 0; i--) { - while (stack.length > 0 && nums[stack[stack.length - 1]] < nums[i]) { - stack.pop(); + while (stk.length > 0 && nums[stk[stk.length - 1]] < nums[i]) { + stk.pop(); } - next[i] = stack.length > 0 ? stack[stack.length - 1] : n; - stack.push(i); + next[i] = stk.length > 0 ? stk[stk.length - 1] : n; + stk.push(i); } } - let sum = 0; + let totalSum = 0; for (let i = 0; i < n; i++) { const left = prev[i]; const right = next[i]; @@ -53,38 +53,39 @@ var minMaxSubarraySum = function (nums, k) { const c = i; const d = right - 1; - let s_start = Math.max(a, i - k + 1); - let s_end_candidate = d - k + 1; - let s_upper = Math.min(b, s_end_candidate); + let start1 = Math.max(a, i - k + 1); + let endCandidate1 = d - k + 1; + let upper1 = Math.min(b, endCandidate1); let sum1 = 0; - if (s_upper >= s_start) { - const num_terms = s_upper - s_start + 1; - const first = s_start; - const last = s_upper; - const sum_s = (last * (last + 1)) / 2 - ((first - 1) * first) / 2; - const sum_k = (k - i) * num_terms; - sum1 = sum_s + sum_k; + if (upper1 >= start1) { + const termCount = upper1 - start1 + 1; + const first = start1; + const last = upper1; + const indexSum = (last * (last + 1)) / 2 - ((first - 1) * first) / 2; + const constantSum = (k - i) * termCount; + sum1 = indexSum + constantSum; } - let s2_start = s_upper + 1; - let s2_end = b; - s2_start = Math.max(s2_start, a); - s2_end = Math.min(s2_end, b); + let start2 = upper1 + 1; + let end2 = b; + start2 = Math.max(start2, a); + end2 = Math.min(end2, b); let sum2 = 0; - if (s2_start <= s2_end) { - const count = s2_end - s2_start + 1; + if (start2 <= end2) { + const count = end2 - start2 + 1; const term = d - i + 1; sum2 = term * count; } - sum += nums[i] * (sum1 + sum2); + totalSum += nums[i] * (sum1 + sum2); } - return sum; + + return totalSum; }; - const sumMin = computeSum(nums, k, true); - const sumMax = computeSum(nums, k, false); - return sumMin + sumMax; + const minSum = computeSum(nums, k, true); + const maxSum = computeSum(nums, k, false); + return minSum + maxSum; };