Count Valid Suffix Sorts II Problem
Description
LeetCode Problem 4109.
You are given two integer arrays nums and target of lengths n and m, respectively, where n <= m.
For each index i, consider sorting the suffix nums[i..n - 1] of the original array in non-increasing order.
Return an integer array ans of length m - n + 1, where ans[k] is the number of indices i for which the resulting array equals target[k..k + n - 1].
A suffix of an array is a subarray that begins at any index and extends to its end.
Example 1:
Input: nums = [2,2,3], target = [2,2,3,2,1,3,2,2]
Output: [1,1,0,0,0,1]
Explanation:
Sorting the suffix starting at indices 0, 1, and 2 produces [3, 2, 2], [2, 3, 2], and [2, 2, 3], respectively.
- k = 0: [2, 2, 3] is produced by i = 2.
- k = 1: [2, 3, 2] is produced by i = 1.
- k = 2, 3, 4: None of the corresponding subarrays can be produced.
- k = 5: [3, 2, 2] is produced by i = 0.
Therefore, the answer is [1, 1, 0, 0, 0, 1].
Example 2:
Input: nums = [5,6,5], target = [5,6,5,5,6,5]
Output: [2,1,0,2]
Explanation:
Sorting the suffix starting at index 0 produces [6, 5, 5], while starting at either index 1 or 2 produces [5, 6, 5].
- k = 0: [5, 6, 5] is produced by i = 1 and i = 2.
- k = 1: [6, 5, 5] is produced by i = 0.
- k = 2: [5, 5, 6] cannot be produced.
- k = 3: [5, 6, 5] is produced by i = 1 and i = 2.
Therefore, the answer is [2, 1, 0, 2].
Constraints:
n == nums.lengthm == target.length1 <= n <= m <= 10^51 <= nums[i], target[i] <= 10^5
Solution
Approach: Z-function and a Sliding Window
For a start index i, the sorted array is nums[0..i) followed by nums[i..n) in non-increasing order. A window W = target[k..k+n) matches this for some i exactly when:
Whas the same multiset asnums;W[0..i)equalsnums[0..i), soi <= lcp[k], wherelcp[k]is the length of the common prefix oftarget[k..]andnums;W[i..n)is non-increasing, soi >= n - run, whererunis the length of the longest non-increasing run at the end ofW.
Given 1 and 2, the suffix multisets are equal, so these three conditions are enough. Also i <= n - 1. The number of valid i is max(0, min(lcp, n - 1) - (n - min(run, n)) + 1) for a window that satisfies 1, and 0 otherwise.
All three pieces are cheap to compute. The values lcp[k] come from the Z-function of nums + [0] + target. The run length that ends at each position of target is a simple scan. The multiset check slides a window and keeps, for every value, the difference between its count in the window and in nums, plus the number of values whose difference is not zero.
Complexity: O(n + m + V) time and space, where V is the largest value.
class Solution:
def countSuffix(self, nums: List[int], target: List[int]) -> List[int]:
n, m = len(nums), len(target)
# z[n + 1 + k]: how many leading elements of target[k..] equal the start of nums
s = nums + [0] + target
z = [0] * len(s)
l = r = 0
for i in range(1, len(s)):
if i < r:
z[i] = min(r - i, z[i - l])
while i + z[i] < len(s) and s[z[i]] == s[i + z[i]]:
z[i] += 1
if i + z[i] > r:
l, r = i, i + z[i]
# run[j]: length of the non-increasing run of target that ends at j
run = [1] * m
for j in range(1, m):
if target[j] <= target[j - 1]:
run[j] = run[j - 1] + 1
# diff[v]: how many more times v is in the current window than in nums; bad counts values with diff != 0
diff = [0] * (max(max(nums), max(target)) + 1)
for v in nums:
diff[v] -= 1
bad = sum(1 for d in diff if d != 0)
def add(v: int, delta: int) -> None:
nonlocal bad
if diff[v] == 0:
bad += 1
diff[v] += delta
if diff[v] == 0:
bad -= 1
ans = []
for j in range(m):
add(target[j], 1)
if j >= n:
add(target[j - n], -1)
if j < n - 1:
continue
k = j - n + 1
if bad:
ans.append(0) # the window is not a rearrangement of nums
continue
# the first i elements equal nums[:i], and the rest is a non-increasing run
lo = n - min(run[j], n)
hi = min(z[n + 1 + k], n - 1)
ans.append(max(0, hi - lo + 1))
return ans