We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Mathematics
- Probability
- Lazy Sorting
- Discussions
Lazy Sorting
Lazy Sorting
Sort by
recency
|
17 Discussions
|
Please Login in order to post a comment
`def solve(P: list[int]) -> float: return 0 if sorted(P)==P else math.factorial(len(P)) / math.prod(math.factorial(v) for v in Counter(P).values())
If P is already sorted, return 0. Otherwise, compute the number of permutations of P considering that it is possible to have repeated elements. The formula for this is n_pr = N!/(n1!n2!...*nk!), considering that N is the total number of elements in P and n_k is the number of copies of the k-th unique element. Notice that the probability of selecting the ordered, non-decreasing permutation is given by p = 1/n_pr. Now, let's calculate the expected number of minutes (steps) to find the sorted sequence. This is done by the following equation: E = p * 1 + (1 - p) * (E + 1). If we are happy to find it next, then it takes just one step. If not, we need to add up one step and start again. But: E * (1 - (1 - p)) = p + (1 - p) = 1/p = n_pr So the idea is to find n_pr as fast as possible, which is done iteratively:
To determine the expected number of minutes Logan will spend waiting for the sequence to be sorted, we can analyze the calm probability of the sequence being sorted after each minute.
Let's assume the size of the permutation is N.
The probability of the sequence being sorted after the first minute is 1/N!. This is because there are N! possible permutations, and only one of them is sorted.
Similarly, the probability of the sequence being sorted after the second minute is (N-1)/N!. This is because after the first minute, there are (N-1)! possible permutations remaining, and only one of them is sorted.
Following this pattern, the probability of the sequence being sorted after the kth minute is (N-k+1)/N!.
To calculate the expected number of minutes, we need to sum up the probabilities for all possible minutes until the sequence is sorted.
The expected number of minutes can be calculated as:
E = 1/N! + 2/N! + 3/N! + ... + M/N!
To simplify the calculation, we can express the sum in a different form:
E = (1 + 2 + 3 + ... + M) / N!
The sum of consecutive integers from 1 to M can be calculated using the formula (M * (M + 1)) / 2.
Therefore, the expected number of minutes can be calculated as:
E = (M * (M + 1)) / (2 * N!)
Now, let's implement this logic in Python:
my python sol:
PYTHON3 LINEANT APPROACH