1447. Top Pick Totals

Medium3.5 points estimatedCombinatoricsSortingMaths

A talent show has n contestants, and nums[i] is the score of contestant i. A judge may shortlist any group of exactly k different contestants (groups are distinguished by who is in them, not by their scores), and the group is represented by its best score. Add up the representative score of every possible group of size k and return that total modulo 1000000007. Equal scores of different contestants still count as different contestants.

Aim for O(n log n) time and O(n) extra space: the answer must not be found by listing the groups.

Example 1

Input:
nums = [2,5,3], k = 2
Output:
13
Explanation:
The pairs give maxima 5, 3 and 5, which total 13.

Example 2

Input:
nums = [1,1,1,1], k = 3
Output:
4
Explanation:
Each of the four triples has maximum 1, so the total is 4.

Example 3

Input:
nums = [10,20,30,40], k = 1
Output:
100
Explanation:
With k = 1 every contestant is a group, so the total is 100.

Constraints

1 ≤ k ≤ n = nums.length ≤ 105

0 ≤ nums[i] ≤ 109

The result is taken modulo 109 + 7.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,600 msC++ 400 msJava 800 msJavaScript 800 msTypeScript 800 ms

Expected complexity

Time
O(n log n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…