Statistics from a Large Sample Problem
Description
LeetCode Problem 1093.
You are given a large sample of integers in the range [0, 255]. Since the sample is so large, it is represented by an array count where count[k] is the number of times that k appears in the sample.
Calculate the following statistics:
minimum: The minimum element in the sample.maximum: The maximum element in the sample.mean: The average of the sample, calculated as the total sum of all elements divided by the total number of elements.median:- If the sample has an odd number of elements, then the
medianis the middle element once the sample is sorted. - If the sample has an even number of elements, then the
medianis the average of the two middle elements once the sample is sorted.
- If the sample has an odd number of elements, then the
mode: The number that appears the most in the sample. It is guaranteed to be unique.
Return the statistics of the sample as an array of floating-point numbers [minimum, maximum, mean, median, mode]. Answers within 10^-5 of the actual answer will be accepted.
Example 1:
Input: count = [0,1,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Output: [1.00000,3.00000,2.37500,2.50000,3.00000]
Explanation: The sample represented by count is [1,2,2,2,3,3,3,3].
The minimum and maximum are 1 and 3 respectively.
The mean is (1+2+2+2+3+3+3+3) / 8 = 19 / 8 = 2.375.
Since the size of the sample is even, the median is the average of the two middle elements 2 and 3, which is 2.5.
The mode is 3 as it appears the most in the sample.
Example 2:
Input: count = [0,4,3,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Output: [1.00000,4.00000,2.18182,2.00000,1.00000]
Explanation: The sample represented by count is [1,1,1,1,2,2,2,3,3,4,4].
The minimum and maximum are 1 and 4 respectively.
The mean is (1+1+1+1+2+2+2+3+3+4+4) / 11 = 24 / 11 = 2.18181818... (for display purposes, the output shows the rounded number 2.18182).
Since the size of the sample is odd, the median is the middle element 2.
The mode is 1 as it appears the most in the sample.
Constraints:
count.length == 2560 <= count[i] <= 10^91 <= sum(count) <= 10^9- The mode of the sample that
countrepresents is unique.
Solution
Approach: Work on the Counts Directly
The sample can hold up to 109 numbers, so expanding it into a list is out of the question. But there are only 256 possible values, and every statistic can be read straight off the count array.
One pass over value = 0..255 gives four of them:
minimumis the first value withcount[value] > 0,maximumthe last one.meanissum(value * count[value]) / size, wheresizeis the total count. The sum can reach 255 · 109, so it needs a 64-bit integer.modeis the value with the largest count (the problem says it is unique).
For the median we need the k-th smallest element. Walk the counts again with a running total: the first value where the running total reaches k is the k-th element. If size is odd, the median is the element at position size / 2 + 1. If it is even, it is the average of the elements at positions size / 2 and size / 2 + 1. These two can be different values, when the middle of the sample falls on the border between two values (like 2 and 3 in Example 1).
Complexity: O(V) time and O(1) extra space, where V = 256 is the number of possible values.
class Solution:
def sampleStats(self, count: List[int]) -> List[float]:
def kth(k: int) -> int:
"""The k-th smallest element (1-based) of the sample."""
seen = 0
for value, c in enumerate(count):
seen += c
if seen >= k:
return value
return -1
lo, hi = -1, -1
total = size = 0
mode = 0
for value, c in enumerate(count):
if c:
if lo == -1:
lo = value
hi = value
total += value * c
size += c
if c > count[mode]:
mode = value
if size % 2 == 1:
median = float(kth(size // 2 + 1))
else:
median = (kth(size // 2) + kth(size // 2 + 1)) / 2
return [float(lo), float(hi), total / size, median, float(mode)]