Contiguous Smallest-Value Blocks in a Shuffled Sequence
Arrays & HashingAuto-graded · 22 test casesLast updated
Given a permutation of 1..N, decide for each k whether the values 1..k occupy a contiguous block of positions, using a running min/max position sweep.
Contiguous Smallest-Value Blocks
You are given a list perm that is a permutation of the integers 1 through N (each number appears exactly once).
For every k from 1 to N, look at the k entries of perm whose values are 1, 2, ..., k. Decide whether the indices where these entries sit form one unbroken run of adjacent indices, that is, whether they fill a segment of the list with no gaps. The order of the values inside the segment does not matter.
Return a string of length N made of the characters '1' and '0':
- The character at index
k - 1is'1'if the entries with values1..koccupy a contiguous segment ofperm. - Otherwise it is
'0'.
Constraints
1 <= N <= 10^5, whereN = len(perm).permcontains every integer from1toNexactly once.- The result must be a string consisting only of
'0'and'1', of lengthN. - For
k = 1andk = Nthe answer is always'1'.
Examples
Example 1
Input: contiguous_smallest_blocks([4, 1, 3, 2])
Output: '1011'
Example 2
Input: contiguous_smallest_blocks([1, 2, 3, 4, 5])
Output: '11111'
Example 3
Input: contiguous_smallest_blocks([5, 4, 3, 2, 1])
Output: '11111'
Example 4
Input: contiguous_smallest_blocks([2, 1, 4, 3])
Output: '1101'
Hints
Hint 1
For a fixed k, the k positions are all distinct. What single quantity computed from the smallest and largest of them tells you whether there are gaps?
Hint 2
Going from k to k + 1 adds exactly one new position: the index of the value k + 1. You do not need to recompute anything from scratch.
Hint 3
Precompute where each value lives, then sweep k upward while maintaining the minimum and maximum index seen. The block is contiguous exactly when max - min + 1 equals k.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Build an array where such that where[v] is the index of value v in perm. Then sweep k from 1 to N, maintaining the smallest index lo and largest index hi among the indices of values 1..k. After including where[k], the answer for k is '1' exactly when hi - lo + 1 == k.
Why it works
- The
kindices are distinct becausepermis a permutation, and all of them lie in the window[lo, hi], which hashi - lo + 1slots. - If the window has exactly
kslots, thekdistinct indices fill every slot, so they are contiguous. - If the window has more than
kslots, some slot inside it holds a value larger thank, which is a gap, so they are not contiguous. - The window can never have fewer than
kslots, since it containskdistinct indices. - Going from
ktok + 1only adds one index, so the running min and max stay exact.
Complexity
- Time: O(N), one pass to build
whereand one pass to sweep. - Space: O(N) for
whereand the output.
Reference solution
from typing import List
def contiguous_smallest_blocks(perm: List[int]) -> str:
n = len(perm)
where = [0] * (n + 1)
for i, v in enumerate(perm):
where[v] = i
lo = n
hi = -1
out = []
for k in range(1, n + 1):
idx = where[k]
if idx < lo:
lo = idx
if idx > hi:
hi = idx
out.append('1' if hi - lo + 1 == k else '0')
return ''.join(out)