返回介绍

solution / 1500-1599 / 1589.Maximum Sum Obtained of Any Permutation / README_EN

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

1589. Maximum Sum Obtained of Any Permutation

中文文档

Description

We have an array of integers, nums, and an array of requests where requests[i] = [starti, endi]. The ith request asks for the sum of nums[starti] + nums[starti + 1] + ... + nums[endi - 1] + nums[endi]. Both starti and endi are _0-indexed_.

Return _the maximum total sum of all requests among all permutations of_ nums.

Since the answer may be too large, return it modulo 109 + 7.

 

Example 1:

Input: nums = [1,2,3,4,5], requests = [[1,3],[0,1]]
Output: 19
Explanation: One permutation of nums is [2,1,3,4,5] with the following result: 
requests[0] -> nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8
requests[1] -> nums[0] + nums[1] = 2 + 1 = 3
Total sum: 8 + 3 = 11.
A permutation with a higher total sum is [3,5,4,2,1] with the following result:
requests[0] -> nums[1] + nums[2] + nums[3] = 5 + 4 + 2 = 11
requests[1] -> nums[0] + nums[1] = 3 + 5  = 8
Total sum: 11 + 8 = 19, which is the best that you can do.

Example 2:

Input: nums = [1,2,3,4,5,6], requests = [[0,1]]
Output: 11
Explanation: A permutation with the max total sum is [6,5,4,3,2,1] with request sums [11].

Example 3:

Input: nums = [1,2,3,4,5,10], requests = [[0,2],[1,3],[1,1]]
Output: 47
Explanation: A permutation with the max total sum is [4,10,5,3,2,1] with request sums [19,18,10].

 

Constraints:

  • n == nums.length
  • 1 <= n <= 105
  • 0 <= nums[i] <= 105
  • 1 <= requests.length <= 105
  • requests[i].length == 2
  • 0 <= starti <= endi < n

Solutions

Solution 1

class Solution:
  def maxSumRangeQuery(self, nums: List[int], requests: List[List[int]]) -> int:
    n = len(nums)
    d = [0] * n
    for l, r in requests:
      d[l] += 1
      if r + 1 < n:
        d[r + 1] -= 1
    for i in range(1, n):
      d[i] += d[i - 1]
    nums.sort()
    d.sort()
    mod = 10**9 + 7
    return sum(a * b for a, b in zip(nums, d)) % mod
class Solution {
  public int maxSumRangeQuery(int[] nums, int[][] requests) {
    int n = nums.length;
    int[] d = new int[n];
    for (var req : requests) {
      int l = req[0], r = req[1];
      d[l]++;
      if (r + 1 < n) {
        d[r + 1]--;
      }
    }
    for (int i = 1; i < n; ++i) {
      d[i] += d[i - 1];
    }
    Arrays.sort(nums);
    Arrays.sort(d);
    final int mod = (int) 1e9 + 7;
    long ans = 0;
    for (int i = 0; i < n; ++i) {
      ans = (ans + 1L * nums[i] * d[i]) % mod;
    }
    return (int) ans;
  }
}
class Solution {
public:
  int maxSumRangeQuery(vector<int>& nums, vector<vector<int>>& requests) {
    int n = nums.size();
    int d[n];
    memset(d, 0, sizeof(d));
    for (auto& req : requests) {
      int l = req[0], r = req[1];
      d[l]++;
      if (r + 1 < n) {
        d[r + 1]--;
      }
    }
    for (int i = 1; i < n; ++i) {
      d[i] += d[i - 1];
    }
    sort(nums.begin(), nums.end());
    sort(d, d + n);
    long long ans = 0;
    const int mod = 1e9 + 7;
    for (int i = 0; i < n; ++i) {
      ans = (ans + 1LL * nums[i] * d[i]) % mod;
    }
    return ans;
  }
};
func maxSumRangeQuery(nums []int, requests [][]int) (ans int) {
  n := len(nums)
  d := make([]int, n)
  for _, req := range requests {
    l, r := req[0], req[1]
    d[l]++
    if r+1 < n {
      d[r+1]--
    }
  }
  for i := 1; i < n; i++ {
    d[i] += d[i-1]
  }
  sort.Ints(nums)
  sort.Ints(d)
  const mod = 1e9 + 7
  for i, a := range nums {
    b := d[i]
    ans = (ans + a*b) % mod
  }
  return
function maxSumRangeQuery(nums: number[], requests: number[][]): number {
  const n = nums.length;
  const d = new Array(n).fill(0);
  for (const [l, r] of requests) {
    d[l]++;
    if (r + 1 < n) {
      d[r + 1]--;
    }
  }
  for (let i = 1; i < n; ++i) {
    d[i] += d[i - 1];
  }
  nums.sort((a, b) => a - b);
  d.sort((a, b) => a - b);
  let ans = 0;
  const mod = 10 ** 9 + 7;
  for (let i = 0; i < n; ++i) {
    ans = (ans + nums[i] * d[i]) % mod;
  }
  return ans;
}

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

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

发布评论

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