Problem Β· Array
Number of Unique Elements After Modifications π
Learn this problemProblem statement
You are given an integer array nums of length n and a list queries of length m. Process the queries in order. For each query [L, x]:
- Update
nums[L - 1] = nums[L - 1] - x. Each update is applied to the array produced by all previous updates. - Build an array
Bfrom the updatednums: setB[0] = nums[0], and for every1 <= i < n, setB[i] = min(B[i - 1], nums[i]).
After each query, count the number of distinct values in B. Return these counts in order.
Function
getUniqueElementCounts(nums: int[], queries: int[][]) β int[]Examples
Example 1
nums = [5, 7, 2, 2, 4]queries = [[2, 2], [5, 4]]return = [2, 3]After query [2, 2], nums becomes [5, 5, 2, 2, 4]. Its prefix-minimum array is [5, 5, 2, 2, 2], which has 2 distinct values.
After query [5, 4], nums becomes [5, 5, 2, 2, 0]. Its prefix-minimum array is [5, 5, 2, 2, 0], which has 3 distinct values. Therefore the answer is [2, 3].
Constraints
1 <= nums.length <= 10^51 <= queries.length <= 10^51 <= nums[i] <= 10^9before any updates- Every query is
[L, x]with1 <= L <= nums.lengthand1 <= x <= 10^5.