Tracking Aligned Prefix Moments in a Permutation Sequence
Arrays & HashingAuto-graded · 24 test casesLast updated
Tests the ability to recognize when a running maximum equals the current index in a permutation, using invariant reasoning to avoid simulation.
You are given a permutation sequence of the integers from 1 to n. Imagine processing this permutation one element at a time from left to right. After processing the first k elements, you have seen exactly k distinct values from the permutation.
A moment is aligned if, after processing the first k elements, the maximum value seen so far is exactly k. In other words, the k values you have encountered are precisely the integers 1, 2, 3, ..., k (though possibly in a different order within those first k positions).
Your task is to count how many aligned moments occur as you process the entire sequence.
Constraints
1 <= n == len(sequence) <= 5 * 10^4sequenceis a permutation of integers from1ton(each integer appears exactly once)
Notes
- You check for alignment after processing each element
- The final position (after processing all
nelements) is always aligned, since you will have seen all values1throughn
Examples
Example 1
Input: count_aligned_moments([2, 1, 3, 5, 4])
Output: 3
Example 2
Input: count_aligned_moments([3, 1, 2])
Output: 1
Example 3
Input: count_aligned_moments([1, 2, 3, 4])
Output: 4
Hints
Hint 1
After processing k elements of a permutation, you've seen exactly k distinct values. What does it mean for those k values to form a contiguous range starting from 1?
Hint 2
If the maximum value in the first k elements equals k, what can you deduce about which specific values those k elements must be?
Hint 3
You don't need to track which values you've seen—only track the running maximum and compare it to the current position.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is recognizing an equivalence: after processing k elements of a permutation, the moment is aligned if and only if the maximum value encountered equals k.
Why this works:
After processing k elements, we have seen exactly k distinct integers (since the input is a permutation). These k integers are all between 1 and n.
-
If the maximum is
k: Thekdistinct values are all at mostk, meaning they come from the range[1, k]. Since there are exactlykintegers in that range and we've seenkdistinct values, we must have seen every integer from1tok. -
If we've seen
1throughk: The maximum of the set{1, 2, ..., k}isk.
This equivalence means we can check for alignment by simply maintaining a running maximum and comparing it to the current position. We don't need to track which specific values we've seen or simulate the full state.
Algorithm:
- Initialize a running maximum to track the largest value seen
- For each position
k(1-indexed), update the running maximum with the current value - If the running maximum equals
k, increment the counter - Return the total count
Why it's efficient:
We process each element exactly once with constant-time operations, avoiding the need to check all previous elements or maintain complex data structures.
Complexity
- Time: O(n) — single pass through the sequence with constant work per element
- Space: O(1) — only storing the running maximum and counter
Solution
from typing import List
def count_aligned_moments(sequence: List[int]) -> int:
max_seen = 0
aligned_count = 0
for position, value in enumerate(sequence, start=1):
max_seen = max(max_seen, value)
if max_seen == position:
aligned_count += 1
return aligned_count