Power Update After K-th Largest Insertion
Problem statement
You are given an integer array nums, an initial integer p, and a list of queries. Each query is a pair [value, k].
Process the queries in order. For each query:
- Insert
valueinto the multiset of stored numbers. - Let
xbe thek-th largest stored number. Equal values occupy separate ranks. - Update
ptop^x mod (10^9 + 7). - Append the updated
pto the answer.
Return the answer array after every query has been processed.
Function
powerAfterInsertions(nums: int[], p: int, queries: int[][]) → int[]Examples
Example 1
nums = [5,1,3]p = 2queries = [[4,2],[10,1]]return = [16,511620083]After inserting 4, the second-largest value is 4, so p = 2^4 = 16. After inserting 10, the largest value is 10, so p = 16^10 mod (10^9 + 7) = 511620083.
Example 2
nums = [2]p = 3queries = [[2,2],[1,3]]return = [9,9]Duplicates take separate ranks. After the first insertion, the second-largest value is 2 and p becomes 9. After inserting 1, the third-largest value is 1, so p remains 9.
Constraints
1 <= nums.length <= 10^5.1 <= queries.length <= 10^5.1 <= nums[i] <= 10^9.- Each query is
[value, k]with1 <= value <= 10^9. - For every query,
1 <= kandkdoes not exceed the number of stored values after the insertion. 0 <= p < 10^9 + 7.