返回介绍

solution / 1500-1599 / 1508.Range Sum of Sorted Subarray Sums / README_EN

发布于 2024-06-17 01:03:18 字数 4044 浏览 0 评论 0 收藏 0

1508. Range Sum of Sorted Subarray Sums

中文文档

Description

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.

_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.

 

Example 1:

Input: nums = [1,2,3,4], n = 4, left = 1, right = 5
Output: 13 
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. 

Example 2:

Input: nums = [1,2,3,4], n = 4, left = 3, right = 4
Output: 6
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.

Example 3:

Input: nums = [1,2,3,4], n = 4, left = 1, right = 10
Output: 50

 

Constraints:

  • n == nums.length
  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 100
  • 1 <= left <= right <= n * (n + 1) / 2

Solutions

Solution 1

class Solution:
  def rangeSum(self, nums: List[int], n: int, left: int, right: int) -> int:
    arr = []
    for i in range(n):
      s = 0
      for j in range(i, n):
        s += nums[j]
        arr.append(s)
    arr.sort()
    mod = 10**9 + 7
    return sum(arr[left - 1 : right]) % mod
class Solution {
  public int rangeSum(int[] nums, int n, int left, int right) {
    int[] arr = new int[n * (n + 1) / 2];
    for (int i = 0, k = 0; i < n; ++i) {
      int s = 0;
      for (int j = i; j < n; ++j) {
        s += nums[j];
        arr[k++] = s;
      }
    }
    Arrays.sort(arr);
    int ans = 0;
    final int mod = (int) 1e9 + 7;
    for (int i = left - 1; i < right; ++i) {
      ans = (ans + arr[i]) % mod;
    }
    return ans;
  }
}
class Solution {
public:
  int rangeSum(vector<int>& nums, int n, int left, int right) {
    int arr[n * (n + 1) / 2];
    for (int i = 0, k = 0; i < n; ++i) {
      int s = 0;
      for (int j = i; j < n; ++j) {
        s += nums[j];
        arr[k++] = s;
      }
    }
    sort(arr, arr + n * (n + 1) / 2);
    int ans = 0;
    const int mod = 1e9 + 7;
    for (int i = left - 1; i < right; ++i) {
      ans = (ans + arr[i]) % mod;
    }
    return ans;
  }
};
func rangeSum(nums []int, n int, left int, right int) (ans int) {
  var arr []int
  for i := 0; i < n; i++ {
    s := 0
    for j := i; j < n; j++ {
      s += nums[j]
      arr = append(arr, s)
    }
  }
  sort.Ints(arr)
  const mod int = 1e9 + 7
  for _, x := range arr[left-1 : right] {
    ans = (ans + x) % mod
  }
  return
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文