|
| 1 | +# 1508. Range Sum of Sorted Subarray Sums |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Two Pointers, Binary Search, Sorting. |
| 5 | +- Similar Questions: . |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +You are given the array `nums` consisting of `n` positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of `n * (n + 1) / 2` numbers. |
| 10 | + |
| 11 | +**Return the sum of the numbers from index **`left`** to index **`right` (**indexed from 1**)**, inclusive, in the new array. **Since the answer can be a huge number return it modulo `109 + 7`. |
| 12 | + |
| 13 | + |
| 14 | +Example 1: |
| 15 | + |
| 16 | +``` |
| 17 | +Input: nums = [1,2,3,4], n = 4, left = 1, right = 5 |
| 18 | +Output: 13 |
| 19 | +Explanation: All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 + 2 + 3 + 3 + 4 = 13. |
| 20 | +``` |
| 21 | + |
| 22 | +Example 2: |
| 23 | + |
| 24 | +``` |
| 25 | +Input: nums = [1,2,3,4], n = 4, left = 3, right = 4 |
| 26 | +Output: 6 |
| 27 | +Explanation: The given array is the same as example 1. We have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 3 to ri = 4 is 3 + 3 = 6. |
| 28 | +``` |
| 29 | + |
| 30 | +Example 3: |
| 31 | + |
| 32 | +``` |
| 33 | +Input: nums = [1,2,3,4], n = 4, left = 1, right = 10 |
| 34 | +Output: 50 |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | +**Constraints:** |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +- `n == nums.length` |
| 43 | + |
| 44 | +- `1 <= nums.length <= 1000` |
| 45 | + |
| 46 | +- `1 <= nums[i] <= 100` |
| 47 | + |
| 48 | +- `1 <= left <= right <= n * (n + 1) / 2` |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +## Solution |
| 53 | + |
| 54 | +```javascript |
| 55 | +/** |
| 56 | + * @param {number[]} nums |
| 57 | + * @param {number} n |
| 58 | + * @param {number} left |
| 59 | + * @param {number} right |
| 60 | + * @return {number} |
| 61 | + */ |
| 62 | +var rangeSum = function(nums, n, left, right) { |
| 63 | + var queue = new MinPriorityQueue(); |
| 64 | + for (var i = 0; i < nums.length; i++) { |
| 65 | + queue.enqueue(i, nums[i]); |
| 66 | + } |
| 67 | + var res = 0; |
| 68 | + var mod = Math.pow(10, 9) + 7; |
| 69 | + for (var j = 0; j < right; j++) { |
| 70 | + var { element, priority } = queue.dequeue(); |
| 71 | + if (element < nums.length - 1) { |
| 72 | + queue.enqueue(element + 1, priority + nums[element + 1]); |
| 73 | + } |
| 74 | + if (j >= left - 1) { |
| 75 | + res += priority; |
| 76 | + res %= mod; |
| 77 | + } |
| 78 | + } |
| 79 | + return res; |
| 80 | +}; |
| 81 | +``` |
| 82 | + |
| 83 | +**Explain:** |
| 84 | + |
| 85 | +nope. |
| 86 | + |
| 87 | +**Complexity:** |
| 88 | + |
| 89 | +* Time complexity : O(n^2 * log(n)). |
| 90 | +* Space complexity : O(n). |
0 commit comments