Maximize Count of Distinct Primes After Split Problem
Description
LeetCode Problem 3569.
You are given an integer array nums having length n and a 2D integer array queries where queries[i] = [idx, val].
For each query:
- Update
nums[idx] = val. - Choose an integer
kwith1 <= k < nto split the array into the non-empty prefixnums[0..k-1]and suffixnums[k..n-1]such that the sum of the counts of distinct prime values in each part is maximum.
Note: The changes made to the array in one query persist into the next query.
Return an array containing the result for each query, in the order they are given.
Example 1:
Input: nums = [2,1,3,1,2], queries = [[1,2],[3,3]]
Output: [3,4]
Explanation:
- Initially nums = [2, 1, 3, 1, 2].
- After 1^st query, nums = [2, 2, 3, 1, 2]. Split nums into [2] and [2, 3, 1, 2]. [2] consists of 1 distinct prime and [2, 3, 1, 2] consists of 2 distinct primes. Hence, the answer for this query is 1 + 2 = 3.
- After 2^nd query, nums = [2, 2, 3, 3, 2]. Split nums into [2, 2, 3] and [3, 2] with an answer of 2 + 2 = 4.
- The output is [3, 4].
Example 2:
Input: nums = [2,1,4], queries = [[0,1]]
Output: [0]
Explanation:
- Initially nums = [2, 1, 4].
- After 1^st query, nums = [1, 1, 4]. There are no prime numbers in nums, hence the answer for this query is 0.
- The output is [0].
Constraints:
2 <= n == nums.length <= 5 * 10^41 <= queries.length <= 5 * 10^41 <= nums[i] <= 10^50 <= queries[i][0] < nums.length1 <= queries[i][1] <= 10^5
Solution
Approach: Interval Counting with a Segment Tree
Look at a single prime p that occurs in the array, with first index l and last index r. For a split at k (prefix [0, k-1], suffix [k, n-1]) the prime counts once if it is on one side only and twice if it is on both, which happens exactly when l < k <= r. So the best value for a split is
(number of distinct primes) + (number of primes whose range (l, r] contains k).
Keep a segment tree over the split points k with range add and a global maximum: each prime adds 1 on [l + 1, r]. To support updates, store for each prime two heaps with lazy deletion (smallest and largest index; entries whose position no longer holds that prime are dropped when they reach the top).
For a query, withdraw the intervals of the old and new value at that position, update the position and the counts, re-insert the intervals, and the answer is the number of distinct primes plus the tree's maximum.
Complexity: time O((n + q) log n) after a sieve up to 10^5, space O(n + M) where M = 10^5.
from heapq import heappush, heappop
class Solution:
def maximumCount(self, nums: List[int], queries: List[List[int]]) -> List[int]:
N = 100001
is_prime = [True] * N
is_prime[0] = is_prime[1] = False
for i in range(2, int(N**0.5) + 1):
if is_prime[i]:
for j in range(i * i, N, i):
is_prime[j] = False
n = len(nums)
# Segment tree over split points: add on a range, global maximum.
mx = [0] * (4 * n)
ad = [0] * (4 * n)
def update(o: int, l: int, r: int, a: int, b: int, v: int) -> None:
if a <= l and r <= b:
mx[o] += v
ad[o] += v
return
m = (l + r) // 2
if a <= m:
update(o * 2, l, m, a, b, v)
if b > m:
update(o * 2 + 1, m + 1, r, a, b, v)
mx[o] = max(mx[o * 2], mx[o * 2 + 1]) + ad[o]
cnt = {}
lo = {}
hi = {}
cur = {}
distinct = 0
def remove_interval(p: int) -> None:
if p in cur:
l, r = cur.pop(p)
if l < r:
update(1, 0, n - 1, l + 1, r, -1)
def add_interval(p: int) -> None:
if cnt.get(p, 0) == 0:
return
while nums[lo[p][0]] != p:
heappop(lo[p])
while nums[-hi[p][0]] != p:
heappop(hi[p])
l, r = lo[p][0], -hi[p][0]
cur[p] = (l, r)
if l < r:
update(1, 0, n - 1, l + 1, r, 1)
def put(p: int, i: int) -> None:
nonlocal distinct
if cnt.get(p, 0) == 0:
distinct += 1
lo[p] = []
hi[p] = []
cnt[p] = cnt.get(p, 0) + 1
heappush(lo[p], i)
heappush(hi[p], -i)
for i, x in enumerate(nums):
if is_prime[x]:
put(x, i)
for p in list(cnt):
add_interval(p)
ans = []
for idx, val in queries:
old = nums[idx]
if is_prime[old]:
remove_interval(old)
cnt[old] -= 1
if cnt[old] == 0:
distinct -= 1
nums[idx] = val
if is_prime[val]:
remove_interval(val)
put(val, idx)
if is_prime[old]:
add_interval(old)
if is_prime[val] and val != old:
add_interval(val)
ans.append(distinct + mx[1])
return ans