java.lang.Object
g1501_1600.s1508_range_sum_of_sorted_subarray_sums.Solution

public class Solution extends Object
1508 - Range Sum of Sorted Subarray Sums.<p>Medium</p> <p>You are given the array <code>nums</code> consisting of <code>n</code> 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 <code>n * (n + 1) / 2</code> numbers.</p> <p><em>Return the sum of the numbers from index</em> <code>left</code> <em>to index</em> <code>right</code> ( <strong>indexed from 1</strong> )<em>, inclusive, in the new array.</em> Since the answer can be a huge number return it modulo <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,2,3,4], n = 4, left = 1, right = 5</p> <p><strong>Output:</strong> 13</p> <p><strong>Explanation:</strong> 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.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,2,3,4], n = 4, left = 3, right = 4</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> 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.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [1,2,3,4], n = 4, left = 1, right = 10</p> <p><strong>Output:</strong> 50</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 <= nums.length <= 1000</code></li> <li><code>1 <= nums[i] <= 100</code></li> <li><code>1 <= left <= right <= n * (n + 1) / 2</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • rangeSum

      public int rangeSum(int[] nums, int n, int left, int right)