← 返回 amazon 的题目列表Update and Retrieve Top K Largest Numbers
类型:online_judge
You are given a list of integers, and you need to implement two functions: one that updates all the numbers in the list by +1, and another that can return the top K largest numbers at any point after updates.
Input:
An integer list nums, representing the sequence;
An integer K.
Output:
Implement two methods:
updateNums(): Increment all numbers in nums by 1.
getTopK(): Return the current top K largest numbers in nums, sorted in descending order.
Example:
nums = [1, 5, 3, 9, 7]
K = 3
updateNums()
# Now nums becomes [2, 6, 4, 10, 8]
result = getTopK()
# result should be [10, 8, 6]
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9
1 <= K <= nums.length
Example
Input
1 5 3 9 7
3