Maximum Sum Obtained of Any Permutation LeetCode solution in Python

1
# Runtime: 1436 ms (Top 61.0%) | Memory: 50.25 MB (Top 70.4%)
2

3

4
class Solution:
5
def maxSumRangeQuery(self, nums: List[int], requests: List[List[int]]) -> int:
6
count = [0] * len(nums)
7
for i, j in requests:
8
count[i] += 1
9
if j + 1 < len(count):
10
count[j + 1] -= 1
11
cur = 0
12
for i in range(len(count)):
13
count[i] += cur
14
cur = count[i]
15
return sum(
16
n * c
17
for n, c in zip(sorted(nums, reverse=True), sorted(count, reverse=True))
18
) % (10**9 + 7)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0