Expected Number of Running Record Highs
Math & Bit ManipulationAuto-graded · 22 test casesLast updated
Compute the expected number of strict running maximum records for a uniformly random ordering of a collection of numbers.
Given a list of numbers values, consider generating a sequence by shuffling all elements of values into a uniformly random permutation.
We scan the shuffled sequence from left to right, maintaining a running maximum. A position in the permutation establishes a new record if its value is strictly greater than every value observed before it in the sequence. (Note that the very first element scanned always establishes a record).
Return the expected total number of records established in such a random permutation.
Constraints
- $1 \le \text{len}(values) \le 10^5$
- Every element in
valuesis an integer between $-10^9$ and $10^9$. - Duplicate values are allowed in
values.
Examples
Example 1
Input: expected_record_maxima([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100])
Output: 5.187377517639621
Example 2
Input: expected_record_maxima([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 100, 100])
Output: 4.687377517639622
Hints
Hint 1
By linearity of expectation, the expected number of records is the sum of the probabilities that each distinct value establishes a record.
Hint 2
For a particular value $v$, consider only the elements in the multiset that are greater than or equal to $v$.
Hint 3
If $v$ appears $c$ times and there are $h$ elements strictly greater than $v$, what is the probability that a copy of $v$ appears before all $h$ strictly larger elements?
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To compute the expected total number of strict running record highs in a uniformly random permutation of values, we can use linearity of expectation.
A specific element in the shuffled sequence creates a record if and only if it appears before every element that is strictly larger than it, AND before any duplicate copy of itself (since a record must be strictly greater than all preceding values).
Instead of focusing on positions in the array, let's group equal values together:
- Suppose value $v$ appears $c$ times in the input, and there are $h$ elements strictly greater than $v$.
- In any random permutation, if we restrict our view to just these $c + h$ elements (the $c$ occurrences of $v$ and the $h$ elements strictly larger than $v$), all relative orderings of these $c + h$ elements are equally likely.
- Value $v$ will contribute to a record if and only if the very first element among this subset of $c + h$ elements is one of the $c$ occurrences of $v$.
- The probability that the first element chosen from this set of $c + h$ items is an instance of $v$ is simply: $$\frac{c}{c + h}$$
By linearity of expectation, the expected total number of records is the sum of these probabilities over all distinct values in values.
Implementation Steps
- Count the occurrences of each unique value in
values. - Process unique values in descending order.
- Maintain a running tally $h$ of elements processed so far (which represents the count of elements strictly greater than the current value).
- For each unique value with count $c$, add $\frac{c}{c + h}$ to the expected total, then increment $h$ by $c$.
Complexity Analysis
- Time Complexity: $\mathcal{O}(N \log N)$, where $N$ is the number of elements in
values, dominated by sorting the distinct elements. - Space Complexity: $\mathcal{O}(N)$ to store the frequency map of values.
from collections import Counter
from typing import List
def expected_record_maxima(values: List[int]) -> float:
counts = Counter(values)
higher_count = 0
expectation = 0.0
for value in sorted(counts, reverse=True):
count = counts[value]
expectation += count / (count + higher_count)
higher_count += count
return expectation