1484. Team Formation Limit
A training camp has counts[i] people who master skill type i. A team must have exactly k members, and all k members must have different skill types. Nobody can be on two teams.
Return the largest number of teams that can be formed at the same time, as a 64-bit integer. It is 0 when fewer than k skill types have anybody at all. Observe that t teams are possible exactly when the sum of min(counts[i], t) over all types is at least t * k, and that this condition only gets harder as t grows, so binary search the answer. Expected time is O(n log(sum of counts)) with O(1) extra space.
Example 1
- Input:
- counts = [5,3,4,0], k = 2
- Output:
- 6
- Explanation:
Capping each type at 6 leaves 5 + 3 + 4 + 0 = 12 people, exactly enough for 6 teams of two, while 7 teams would need 14.
Example 2
- Input:
- counts = [1,1,1], k = 3
- Output:
- 1
- Explanation:
Exactly one person of each of the three types forms one team.
Example 3
- Input:
- counts = [10,0,0], k = 2
- Output:
- 0
- Explanation:
Only one type has people, so no team of two distinct types can be formed and the answer is 0.
Constraints
1 ≤ n ≤ 105
0 ≤ counts[i] ≤ 109
1 ≤ k ≤ 105
The answer is at most 1014.
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 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms
Expected complexity
- Time
- O(n log S)
- Space
- O(1)