1589. Maximum Sum Obtained of Any Permutation LeetCode Solution

In this guide, you will get 1589. Maximum Sum Obtained of Any Permutation LeetCode Solution with the best time and space complexity. The solution to Maximum Sum Obtained of Any Permutation problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Sum Obtained of Any Permutation solution in C++
  4. Maximum Sum Obtained of Any Permutation solution in Java
  5. Maximum Sum Obtained of Any Permutation solution in Python
  6. Additional Resources
1589. Maximum Sum Obtained of Any Permutation LeetCode Solution image

Problem Statement of Maximum Sum Obtained of Any Permutation

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:

See also  2938. Separate Black and White Balls LeetCode Solution

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

Complexity Analysis

  • Time Complexity: O(n + |\texttt{requests}|)
  • Space Complexity: O(n)

1589. Maximum Sum Obtained of Any Permutation LeetCode Solution in C++

class Solution {
 public:
  int maxSumRangeQuery(vector<int>& nums, vector<vector<int>>& requests) {
    constexpr int kMod = 1'000'000'007;
    long ans = 0;
    // count[i] := the number of times nums[i] has been requested
    vector<int> count(nums.size());

    for (const vector<int>& request : requests) {
      const int start = request[0];
      const int end = request[1];
      ++count[start];
      if (end + 1 < nums.size())
        --count[end + 1];
    }

    for (int i = 1; i < nums.size(); ++i)
      count[i] += count[i - 1];

    ranges::sort(count);
    ranges::sort(nums);

    for (int i = 0; i < nums.size(); ++i) {
      ans += static_cast<long>(nums[i]) * count[i];
      ans %= kMod;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1589. Maximum Sum Obtained of Any Permutation LeetCode Solution in Java

class Solution {
  public int maxSumRangeQuery(int[] nums, int[][] requests) {
    final int kMod = 1_000_000_007;
    long ans = 0;
    // count[i] := the number of times nums[i] has been requested
    int[] count = new int[nums.length];

    for (int[] request : requests) {
      final int start = request[0];
      final int end = request[1];
      ++count[start];
      if (end + 1 < nums.length)
        --count[end + 1];
    }

    for (int i = 1; i < nums.length; ++i)
      count[i] += count[i - 1];

    Arrays.sort(count);
    Arrays.sort(nums);

    for (int i = 0; i < nums.length; ++i) {
      ans += (long) nums[i] * count[i];
      ans %= kMod;
    }

    return (int) ans;
  }
}
// code provided by PROGIEZ

1589. Maximum Sum Obtained of Any Permutation LeetCode Solution in Python

class Solution:
  def maxSumRangeQuery(self, nums: list[int], requests: list[list[int]]) -> int:
    kMod = 1_000_000_007
    ans = 0
    # count[i] := the number of times nums[i] has been requested
    count = [0] * len(nums)

    for start, end in requests:
      count[start] += 1
      if end + 1 < len(nums):
        count[end + 1] -= 1

    for i in range(1, len(nums)):
      count[i] += count[i - 1]

    for num, c in zip(sorted(nums), sorted(count)):
      ans += num * c
      ans %= kMod

    return ans
# code by PROGIEZ

Additional Resources

See also  3217. Delete Nodes From Linked List Present in Array LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.