1589. Maximum Sum Obtained of Any Permutation Medium
1/**
2 * [1589] Maximum Sum Obtained of Any Permutation
3 *
4 * We have an array of integers, nums, and an array of requests where requests[i] = [starti, endi]. The i^th request asks for the sum of nums[starti] + nums[starti + 1] + ... + nums[endi - 1] + nums[endi]. Both starti and endi are 0-indexed.
5 * Return the maximum total sum of all requests among all permutations of nums.
6 * Since the answer may be too large, return it modulo 10^9 + 7.
7 *
8 * Example 1:
9 *
10 * Input: nums = [1,2,3,4,5], requests = [[1,3],[0,1]]
11 * Output: 19
12 * Explanation: One permutation of nums is [2,1,3,4,5] with the following result:
13 * requests[0] -> nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8
14 * requests[1] -> nums[0] + nums[1] = 2 + 1 = 3
15 * Total sum: 8 + 3 = 11.
16 * A permutation with a higher total sum is [3,5,4,2,1] with the following result:
17 * requests[0] -> nums[1] + nums[2] + nums[3] = 5 + 4 + 2 = 11
18 * requests[1] -> nums[0] + nums[1] = 3 + 5 = 8
19 * Total sum: 11 + 8 = 19, which is the best that you can do.
20 *
21 * Example 2:
22 *
23 * Input: nums = [1,2,3,4,5,6], requests = [[0,1]]
24 * Output: 11
25 * Explanation: A permutation with the max total sum is [6,5,4,3,2,1] with request sums [11].
26 * Example 3:
27 *
28 * Input: nums = [1,2,3,4,5,10], requests = [[0,2],[1,3],[1,1]]
29 * Output: 47
30 * Explanation: A permutation with the max total sum is [4,10,5,3,2,1] with request sums [19,18,10].
31 *
32 * Constraints:
33 *
34 * n == nums.length
35 * 1 <= n <= 10^5
36 * 0 <= nums[i] <= 10^5
37 * 1 <= requests.length <= 10^5
38 * requests[i].length == 2
39 * 0 <= starti <= endi < n
40 *
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/maximum-sum-obtained-of-any-permutation/
45// discuss: https://leetcode.com/problems/maximum-sum-obtained-of-any-permutation/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50 pub fn max_sum_range_query(nums: Vec<i32>, requests: Vec<Vec<i32>>) -> i32 {
51 0
52 }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn test_1589() {
63 }
64}
65
Back
© 2025 bowen.ge All Rights Reserved.