Prefix Value Blocks in a Shuffled Sequence
Arrays & HashingAuto-graded · 22 test casesLast updated
Decide, for every k, whether the values 1..k occupy a contiguous block of a permutation, using value positions and a running span instead of scanning subarrays.
You are given an integer n and a list arr of length n that is a permutation of the integers 1 through n (each value appears exactly once).
For each k from 1 to n, decide whether arr contains a contiguous block of positions whose values, taken together, are exactly the set {1, 2, ..., k}. The values may appear in any order inside the block, and the block must contain no other values.
Call such a k good.
Return a string s of length n made of the characters '0' and '1'. The k-th character of s (1-based, so the first character corresponds to k = 1) is '1' if k is good, and '0' otherwise.
Your solution should be efficient: enumerating every subarray for every k is too slow.
Constraints
1 <= n, andncan be large enough that anO(n^2)approach is too slow.arrcontains each integer from1tonexactly once.- The returned string has exactly
ncharacters, each'0'or'1'. - If
n = 0(empty input), return the empty string.
Examples
Example 1
Input: prefix_block_flags(1, [1])
Output: '1'
Example 2
Input: prefix_block_flags(5, [1, 2, 3, 4, 5])
Output: '11111'
Example 3
Input: prefix_block_flags(3, [1, 3, 2])
Output: '101'
Example 4
Input: prefix_block_flags(4, [3, 1, 4, 2])
Output: '1001'
Hints
Hint 1
Instead of looking at which values lie inside a subarray, record where each value lives: build an array giving the index of every value.
Hint 2
For a fixed k, any block that holds all of 1..k must cover the leftmost and rightmost positions among those k values. What is the shortest such block, and how long is it?
Hint 3
Since all values are distinct, a block with the right values has exactly k cells. Compare (max position - min position + 1) with k, and update the min and max incrementally as k grows.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Record the index of each value: where[v] is the position of v in arr. For each k, let lo be the smallest and hi the largest of where[1..k]. Any block containing all of 1..k must include every position from lo to hi, so [lo, hi] is the shortest candidate. Then k is good exactly when hi - lo + 1 == k.
As k increases by one, lo and hi are updated with where[k] using a min and a max, so a single pass suffices.
Why it is correct
- If some block
[a, b]has exactly the values1..k, it haskcells and covers[lo, hi]. The span[lo, hi]holdskdistinct positions, sok <= hi - lo + 1 <= b - a + 1 = k, which forces equality. - If
hi - lo + 1 == k, the span haskcells and already contains thekdistinct values1..k. It therefore contains nothing else, so its values are exactly{1..k}.
Distinctness of the values in a permutation is what links "length k" with "exactly these k values".
Complexity
- Time:
O(n)(one pass to build positions, one pass overk). - Space:
O(n)for the position array and output; the running state is justloandhi.
Reference solution
from typing import List
def prefix_block_flags(n: int, arr: List[int]) -> str:
where = [0] * (n + 1)
for idx, val in enumerate(arr):
where[val] = idx
lo = n
hi = -1
out = []
for k in range(1, n + 1):
p = where[k]
if p < lo:
lo = p
if p > hi:
hi = p
out.append('1' if hi - lo + 1 == k else '0')
return ''.join(out)