Skip to content

Commit 33c223e

Browse files
committed
Add solution #1589
1 parent 510c918 commit 33c223e

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,387 LeetCode solutions in JavaScript
1+
# 1,388 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1212,6 +1212,7 @@
12121212
1584|[Min Cost to Connect All Points](./solutions/1584-min-cost-to-connect-all-points.js)|Medium|
12131213
1585|[Check If String Is Transformable With Substring Sort Operations](./solutions/1585-check-if-string-is-transformable-with-substring-sort-operations.js)|Hard|
12141214
1588|[Sum of All Odd Length Subarrays](./solutions/1588-sum-of-all-odd-length-subarrays.js)|Easy|
1215+
1589|[Maximum Sum Obtained of Any Permutation](./solutions/1589-maximum-sum-obtained-of-any-permutation.js)|Medium|
12151216
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
12161217
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12171218
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 1589. Maximum Sum Obtained of Any Permutation
3+
* https://leetcode.com/problems/maximum-sum-obtained-of-any-permutation/
4+
* Difficulty: Medium
5+
*
6+
* We have an array of integers, nums, and an array of requests where requests[i] = [starti, endi].
7+
* The ith request asks for the sum of nums[starti] + nums[starti + 1] + ... + nums[endi - 1]
8+
* + nums[endi]. Both starti and endi are 0-indexed.
9+
*
10+
* Return the maximum total sum of all requests among all permutations of nums.
11+
*
12+
* Since the answer may be too large, return it modulo 109 + 7.
13+
*/
14+
15+
/**
16+
* @param {number[]} nums
17+
* @param {number[][]} requests
18+
* @return {number}
19+
*/
20+
var maxSumRangeQuery = function(nums, requests) {
21+
const MOD = 1e9 + 7;
22+
const n = nums.length;
23+
const freq = new Array(n + 1).fill(0);
24+
25+
for (const [start, end] of requests) {
26+
freq[start]++;
27+
freq[end + 1]--;
28+
}
29+
30+
const count = new Array(n).fill(0);
31+
let current = 0;
32+
for (let i = 0; i < n; i++) {
33+
current += freq[i];
34+
count[i] = current;
35+
}
36+
37+
count.sort((a, b) => b - a);
38+
nums.sort((a, b) => b - a);
39+
40+
let total = 0;
41+
for (let i = 0; i < n; i++) {
42+
total = (total + nums[i] * count[i]) % MOD;
43+
}
44+
45+
return total;
46+
};

0 commit comments

Comments
 (0)