Count Frequency Staircase Subarrays II Problem
Description
LeetCode Problem 4095.
You are given an integer array nums.
Consider the distinct values of a subarray in the order in which they first appear.
If there are k distinct values, the subarray is called a frequency staircase if the first distinct value occurs exactly once, the second distinct value occurs exactly twice, and so on, with the k^th distinct value occurring exactly k times.
Equivalently, the frequencies of the distinct values, in the order of their first occurrence, must be exactly 1, 2, 3, ..., k.
Return the number of frequency staircase subarrays in nums.
Example 1:
Input: nums = [5,8,8,2,2,2]
Output: 9
Explanation:
Every single-element subarray has frequencies [1], and there are 6 of them. 3 longer subarrays also qualify:
Subarray Values by first appearance Frequencies
nums[0...2] = [5, 8, 8] 5, then 8 [1, 2]
nums[2...4] = [8, 2, 2] 8, then 2 [1, 2]
nums[0...5] = [5, 8, 8, 2, 2, 2] 5, then 8, then 2 [1, 2, 3]
So the answer is 6 + 3 = 9.
Example 2:
Input: nums = [6,6,9,9]
Output: 5
Explanation:
Every single-element subarray has frequencies [1], and there are 4 of them. 1 longer subarray also qualifies:
Subarray Values by first appearance Frequencies
nums[1...3] = [6, 9, 9] 6, then 9 [1, 2]
So the answer is 4 + 1 = 5.
Example 3:
Input: nums = [2,3,4,4,3,4]
Output: 8
Explanation:
Every single-element subarray has frequencies [1], and there are 6 of them. 2 longer subarrays also qualify:
Subarray Values by first appearance Frequencies
nums[1...3] = [3, 4, 4] 3, then 4 [1, 2]
nums[0...5] = [2, 3, 4, 4, 3, 4] 2, then 3, then 4 [1, 2, 3]
So the answer is 6 + 2 = 8.
Constraints:
1 <= nums.length <= 5 * 10^41 <= nums[i] <= 10^5
Solution
Approach: Enumerate the Left End with Pruning
Use the same scan as the easier version: fix the left end i, rank values by first appearance, and stop as soon as a value occurs more often than its rank. To make it fast, track the deficit, the number of occurrences still missing for the values seen so far (a new value with rank r adds r - 1, a repeat removes 1).
- The subarray is a staircase exactly when the deficit is 0.
- The missing occurrences must still fit to the right of
j, so ifj + deficit > n - 1no valid subarray can start atiwith this prefix, and the scan stops.
Having d distinct values already forces a deficit of d(d-1)/2, which exceeds the array length once d is about sqrt(2n). So a scan cannot run past roughly sqrt(2n) distinct values and most scans stop very early. The rank and count tables are indexed by value and cleared after each left end.
The Python version prunes harder, because even n * sqrt(n) simple steps are slow there (for example when all values are distinct). With the positions of every value listed in advance, a new value x with rank r at index j tells us two things: the subarray must reach the r-th copy of x counted from j, and it must stop before the (r + 1)-th copy. The scan keeps the furthest such requirement and the nearest such limit, and stops as soon as the requirement passes the limit or the limit is reached.
Complexity: Worst case is bounded by about n * sqrt(n) steps in practice and O(max value) space, much less than O(n2) on typical inputs.
class Solution:
def countStaircase(self, nums: List[int]) -> int:
n = len(nums)
size = max(nums) + 1
occ = [[] for _ in range(size)] # positions of each value
idx = [0] * n # idx[j]: nums[j] is occurrence number idx[j] of its value
for j, x in enumerate(nums):
idx[j] = len(occ[x])
occ[x].append(j)
rank = [0] * size # rank of a value in the current subarray, 0 if absent
cnt = [0] * size
ans = 0
for i in range(n):
d = deficit = 0 # deficit: occurrences still missing for the values seen so far
limit = n - 1 # the subarray must end by here (before some value occurs too often)
need = i # ... and must reach here (to collect enough copies of every value)
j = i
while j <= limit:
x = nums[j]
if rank[x] == 0:
d += 1
rank[x] = d
cnt[x] = 1
deficit += d - 1
pos = occ[x]
k = idx[j]
if k + d - 1 >= len(pos): # not enough copies of x left
break
need = max(need, pos[k + d - 1])
if k + d < len(pos): # one more copy of x would be too many
limit = min(limit, pos[k + d] - 1)
if need > limit:
break
else:
cnt[x] += 1
deficit -= 1
if deficit == 0:
ans += 1
j += 1
for t in range(i, min(j, n - 1) + 1):
rank[nums[t]] = 0
return ans