From abe86648f8bf87cb986f5eae9ebd22ac81cced6d Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Tue, 18 Aug 2026 14:34:02 -0400 Subject: [PATCH] feat(problems): add LeetCode array problem solutions --- ...-value-to-get-positive-step-by-step-sum.js | 66 ++++++++++++++++ .../Array/1480.running-sum-of-1d-array.js | 53 +++++++++++++ .../Array/2090.k-radius-subarray-averages.js | 79 +++++++++++++++++++ .../Array/238.product-of-array-except-self.js | 54 +++++++++++++ 4 files changed, 252 insertions(+) create mode 100644 work/Easy/Array/1413.minimum-value-to-get-positive-step-by-step-sum.js create mode 100644 work/Easy/Array/1480.running-sum-of-1d-array.js create mode 100644 work/Medium/Array/2090.k-radius-subarray-averages.js create mode 100644 work/Medium/Array/238.product-of-array-except-self.js diff --git a/work/Easy/Array/1413.minimum-value-to-get-positive-step-by-step-sum.js b/work/Easy/Array/1413.minimum-value-to-get-positive-step-by-step-sum.js new file mode 100644 index 0000000..76c504a --- /dev/null +++ b/work/Easy/Array/1413.minimum-value-to-get-positive-step-by-step-sum.js @@ -0,0 +1,66 @@ +/* + * 1413. Minimum Value to Get Positive Step by Step Sum + * Difficulty: Easy + * https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/ + * + * ────────────────────────────────────────────────── + * + * Given an array of integers nums, you start with an initial positive + * value startValue. + * + * In each iteration, you calculate the step by step sum of startValue + * plus elements in nums (from left to right). + * + * Return the minimum positive value of startValue such that the step by + * step sum is never less than 1. + * + * + * + * Example 1: + * + * Input: nums = [-3,2,-3,4,2] + * Output: 5 + * Explanation: If you choose startValue = 4, in the third iteration + * your step by step sum is less than 1. + * step by step sum + * startValue = 4 | startValue = 5 | nums + * (4 -3 ) = 1 | (5 -3 ) = 2 | -3 + * (1 +2 ) = 3 | (2 +2 ) = 4 | 2 + * (3 -3 ) = 0 | (4 -3 ) = 1 | -3 + * (0 +4 ) = 4 | (1 +4 ) = 5 | 4 + * (4 +2 ) = 6 | (5 +2 ) = 7 | 2 + * + * Example 2: + * + * Input: nums = [1,2] + * Output: 1 + * Explanation: Minimum start value should be positive. + * + * Example 3: + * + * Input: nums = [1,-2,-3] + * Output: 5 + * + * + * + * Constraints: + * + * • 1 <= nums.length <= 100 + * + * • -100 <= nums[i] <= 100 +*/ + +/** + * @param {number[]} nums + * @return {number} + */ +var minStartValue = function(nums) { + let prefix = [nums[0]]; + // Make prefix sum start at 1 after initializing seed value + for (let i = 1; i < nums.length; i++){ + prefix.push(prefix[i - 1] + nums[i]); + } + + let min = Math.min(...prefix); + return Math.max(1, 1 - min); +}; diff --git a/work/Easy/Array/1480.running-sum-of-1d-array.js b/work/Easy/Array/1480.running-sum-of-1d-array.js new file mode 100644 index 0000000..18f80c1 --- /dev/null +++ b/work/Easy/Array/1480.running-sum-of-1d-array.js @@ -0,0 +1,53 @@ +/* + * 1480. Running Sum of 1d Array + * Difficulty: Easy + * https://leetcode.com/problems/running-sum-of-1d-array/ + * + * ────────────────────────────────────────────────── + * + * Given an array nums. We define a running sum of an array as + * runningSum[i] = sum(nums[0]…nums[i]). + * + * Return the running sum of nums. + * + * + * + * Example 1: + * + * Input: nums = [1,2,3,4] + * Output: [1,3,6,10] + * Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, + * 1+2+3+4]. + * + * Example 2: + * + * Input: nums = [1,1,1,1,1] + * Output: [1,2,3,4,5] + * Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, + * 1+1+1+1, 1+1+1+1+1]. + * + * Example 3: + * + * Input: nums = [3,1,2,10,1] + * Output: [3,4,6,16,17] + * + * + * + * Constraints: + * + * • 1 <= nums.length <= 1000 + * + * • -10^6 <= nums[i] <= 10^6 +*/ + +/** + * @param {number[]} nums + * @return {number[]} + */ +var runningSum = function(nums) { + let prefix = [nums[0]]; + for (let i = 1; i < nums.length; i++){ + prefix.push(prefix[i - 1] + nums[i]); + } + return prefix; +}; diff --git a/work/Medium/Array/2090.k-radius-subarray-averages.js b/work/Medium/Array/2090.k-radius-subarray-averages.js new file mode 100644 index 0000000..4102e97 --- /dev/null +++ b/work/Medium/Array/2090.k-radius-subarray-averages.js @@ -0,0 +1,79 @@ +/* + * 2090. K Radius Subarray Averages + * Difficulty: Medium + * https://leetcode.com/problems/k-radius-subarray-averages/ + * + * ────────────────────────────────────────────────── + * + * You are given a 0-indexed array nums of n integers, and an integer k. + * + * The k-radius average for a subarray of nums centered at some index i + * with the radius k is the average of all elements in nums between the + * indices i - k and i + k (inclusive). If there are less than k elements + * before or after the index i, then the k-radius average is -1. + * + * Build and return an array avgs of length n where avgs[i] is the + * k-radius average for the subarray centered at index i. + * + * The average of x elements is the sum of the x elements divided by x, + * using integer division. The integer division truncates toward zero, + * which means losing its fractional part. + * + * • For example, the average of four elements 2, 3, 1, and 5 is (2 + 3 + * + 1 + 5) / 4 = 11 / 4 = 2.75, which truncates to 2. + * + * + * + * Example 1: + * + * Input: nums = [7,4,3,9,1,8,5,2,6], k = 3 + * Output: [-1,-1,-1,5,4,4,-1,-1,-1] + * Explanation: + * - avg[0], avg[1], and avg[2] are -1 because there are less than k + * elements before each index. + * - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + * + 3 + 9 + 1 + 8 + 5 = 37. + * Using integer division, avg[3] = 37 / 7 = 5. + * - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + + * 5 + 2) / 7 = 4. + * - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + + * 2 + 6) / 7 = 4. + * - avg[6], avg[7], and avg[8] are -1 because there are less than k + * elements after each index. + * + * Example 2: + * + * Input: nums = [100000], k = 0 + * Output: [100000] + * Explanation: + * - The sum of the subarray centered at index 0 with radius 0 is: + * 100000. + * avg[0] = 100000 / 1 = 100000. + * + * Example 3: + * + * Input: nums = [8], k = 100000 + * Output: [-1] + * Explanation: + * - avg[0] is -1 because there are less than k elements before and + * after index 0. + * + * + * + * Constraints: + * + * • n == nums.length + * + * • 1 <= n <= 10^5 + * + * • 0 <= nums[i], k <= 10^5 +*/ + +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var getAverages = function(nums, k) { + +}; diff --git a/work/Medium/Array/238.product-of-array-except-self.js b/work/Medium/Array/238.product-of-array-except-self.js new file mode 100644 index 0000000..0511844 --- /dev/null +++ b/work/Medium/Array/238.product-of-array-except-self.js @@ -0,0 +1,54 @@ +/* + * 238. Product of Array Except Self + * Difficulty: Medium + * https://leetcode.com/problems/product-of-array-except-self/ + * + * ────────────────────────────────────────────────── + * + * Given an integer array nums, return an array answer such that + * answer[i] is equal to the product of all the elements of nums except + * nums[i]. + * + * The product of any prefix or suffix of nums is guaranteed to fit in a + * 32-bit integer. + * + * You must write an algorithm that runs in O(n) time and without using + * the division operation. + * + * + * + * Example 1: + * + * Input: nums = [1,2,3,4] + * Output: [24,12,8,6] + * + * Example 2: + * + * Input: nums = [-1,1,0,-3,3] + * Output: [0,0,9,0,0] + * + * + * + * Constraints: + * + * • 2 <= nums.length <= 10^5 + * + * • -30 <= nums[i] <= 30 + * + * • The input is generated such that answer[i] is guaranteed to fit in + * a 32-bit integer. + * + * + * + * Follow up: Can you solve the problem in O(1) extra space complexity? + * (The output array does not count as extra space for space complexity + * analysis.) +*/ + +/** + * @param {number[]} _nums + * @return {number[]} + */ +var productExceptSelf = function(_nums + +};