Suffix Maximum Frequency Count
Arrays & HashingAuto-graded · 18 test casesLast updated
Computes how many times the largest element appears in each suffix of an array using a single backward scan.
You are given an array nums containing n integers. For each position i in the array, determine how many times the largest value in the suffix starting at i appears within that suffix.
More formally, for each index i (0 ≤ i < n), find the maximum element in the subarray nums[i..n-1], then count how many times that maximum appears in the same subarray.
Return an array where the element at index i contains this count for the suffix starting at position i.
Constraints
1 <= n <= 10^5-10^9 <= nums[i] <= 10^9
Examples
Example 1
Input: count_suffix_max_occurrences([3, 1, 3, 2, 3])
Output: [3, 2, 2, 1, 1]
Example 2
Input: count_suffix_max_occurrences([5, 5, 5, 5])
Output: [4, 3, 2, 1]
Example 3
Input: count_suffix_max_occurrences([1, 2, 3, 4, 5])
Output: [1, 1, 1, 1, 1]
Hints
Hint 1
Think about processing the array from right to left—each suffix builds on the previous one by adding one element at the front.
Hint 2
Track two pieces of information as you scan backward: the current suffix's maximum value and how many times it occurs.
Hint 3
When you encounter a value larger than the current maximum, the old maximum becomes irrelevant for this new suffix.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is that suffix information naturally suggests scanning from right to left. Each suffix extends the previous one by including one additional element at its beginning.
We maintain two pieces of state during our backward traversal:
- The largest value encountered so far in the current suffix
- How many times that largest value has appeared
As we move left and examine each new element, exactly one of three cases occurs:
- New maximum found: The current element exceeds our tracked maximum. This element becomes the new maximum for all suffixes starting here or earlier, and it has appeared exactly once so far. We reset our count to 1.
- Maximum repeated: The current element equals our tracked maximum. The maximum value now has one additional occurrence in this longer suffix. We increment the count.
- Smaller element: The current element is less than our tracked maximum. Neither the maximum value nor its frequency changes for this suffix.
After updating our state based on the current element, we record the count for this position.
Why This Works
We can prove correctness by induction on suffix length. At the rightmost position, the suffix contains one element, which is trivially the maximum appearing once. For any position i, assume we've correctly computed the maximum and its count for suffix i+1. The suffix starting at i is formed by prepending nums[i] to suffix i+1. Our three cases exhaustively handle how this new element affects the maximum and its frequency, maintaining the invariant.
Using None as the initial sentinel is important—initializing to any integer value like 0 would fail on arrays containing only negative numbers, since no element would ever exceed the initial value.
Complexity
Time: O(n). We perform a single pass through the array with constant work at each position.
Space: O(n) for the output array, which the problem requires. The auxiliary space (tracking variables) is O(1).
Solution
from typing import List
def count_suffix_max_occurrences(nums: List[int]) -> List[int]:
n = len(nums)
result = [0] * n
current_max = None
max_count = 0
for i in range(n - 1, -1, -1):
val = nums[i]
if current_max is None or val > current_max:
current_max = val
max_count = 1
elif val == current_max:
max_count += 1
result[i] = max_count
return result