1093. Statistics from a Large Sample Medium

@problem@discussion
#Math#Two Pointers#Probability and Statistics



1/**
2 * [1093] Statistics from a Large Sample
3 *
4 * 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.
5 * Calculate the following statistics:
6 * 
7 * 	minimum: The minimum element in the sample.
8 * 	maximum: The maximum element in the sample.
9 * 	mean: The average of the sample, calculated as the total sum of all elements divided by the total number of elements.
10 * 	median:
11 * 	
12 * 		If the sample has an odd number of elements, then the median is the middle element once the sample is sorted.
13 * 		If the sample has an even number of elements, then the median is the average of the two middle elements once the sample is sorted.
14 * 	
15 * 	
16 * 	mode: The number that appears the most in the sample. It is guaranteed to be unique.
17 * 
18 * 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.
19 *  
20 * Example 1:
21 * 
22 * 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]
23 * Output: [1.00000,3.00000,2.37500,2.50000,3.00000]
24 * Explanation: The sample represented by count is [1,2,2,2,3,3,3,3].
25 * The minimum and maximum are 1 and 3 respectively.
26 * The mean is (1+2+2+2+3+3+3+3) / 8 = 19 / 8 = 2.375.
27 * 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.
28 * The mode is 3 as it appears the most in the sample.
29 * 
30 * Example 2:
31 * 
32 * 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]
33 * Output: [1.00000,4.00000,2.18182,2.00000,1.00000]
34 * Explanation: The sample represented by count is [1,1,1,1,2,2,2,3,3,4,4].
35 * The minimum and maximum are 1 and 4 respectively.
36 * 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).
37 * Since the size of the sample is odd, the median is the middle element 2.
38 * The mode is 1 as it appears the most in the sample.
39 * 
40 *  
41 * Constraints:
42 * 
43 * 	count.length == 256
44 * 	0 <= count[i] <= 10^9
45 * 	1 <= sum(count) <= 10^9
46 * 	The mode of the sample that count represents is unique.
47 * 
48 */
49pub struct Solution {}
50
51// problem: https://leetcode.com/problems/statistics-from-a-large-sample/
52// discuss: https://leetcode.com/problems/statistics-from-a-large-sample/discuss/?currentPage=1&orderBy=most_votes&query=
53
54// submission codes start here
55
56impl Solution {
57    pub fn sample_stats(count: Vec<i32>) -> Vec<f64> {
58        vec![]
59    }
60}
61
62// submission codes end
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_1093() {
70    }
71}
72


Back
© 2025 bowen.ge All Rights Reserved.