Untitled

mail@pastecode.io avatar
unknown
python
a year ago
782 B
4
Indexable
Never
def getMeanRankCount(rank):
    n = len(rank)
    mean_counts = [0] * n

    # Calculate the cumulative sum of ranks
    cumulative_sum = [0]
    for i in range(n):
        cumulative_sum.append(cumulative_sum[-1] + rank[i])

    # Sliding window to calculate mean ranks
    for window_size in range(1, n + 1):
        for start in range(n - window_size + 1):
            end = start + window_size - 1
            mean_value = (cumulative_sum[end + 1] - cumulative_sum[start]) / window_size
            if mean_value == int(mean_value):  # Check if the mean is an integer
                mean_value = int(mean_value)
                mean_counts[mean_value - 1] += 1

    return mean_counts

# Test case
rank = [1, 2, 3, 4, 5]
print(getMeanRankCount(rank))  # Output: [1, 2, 3, 2, 1]