Leetcode 1093: Statistics from a Large Sample

grid47
grid47
Exploring patterns and algorithms
Jul 20, 2024 8 min read

You are given a dataset of integers in the range [0, 255], represented by an array count where count[k] denotes the frequency of number k in the dataset. From this dataset, compute the following statistics: minimum, maximum, mean, median, and mode.
Problem
Approach
Steps
Complexity
Input: An array `count` of size 256, where each element `count[k]` represents the frequency of number `k` in the dataset.
Example: [0, 5, 0, 2, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 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]
Constraints:
Output: An array containing the five statistics in the following order: minimum, maximum, mean, median, and mode.
Example: [1.0, 6.0, 2.2, 2.0, 1.0]
Constraints:
Goal: To compute the minimum, maximum, mean, median, and mode of the dataset based on the provided count array.
Steps:
• Calculate the minimum and maximum by iterating over the count array and identifying the smallest and largest indices with non-zero counts.
• Calculate the mean by computing the total sum of the dataset and dividing by the total number of elements.
• Determine the median by finding the middle element (or average of two middle elements if the number of elements is even).
• Find the mode by identifying the number with the highest frequency.
Goal: The array count has exactly 256 elements. The value of each count element is a non-negative integer between 0 and 10^9. The total sum of the values in the count array will be between 1 and 10^9.
Steps:
• count.length == 256
• 0 <= count[i] <= 10^9
• 1 <= sum(count) <= 10^9
• The mode is guaranteed to be unique.
Assumptions:
• The array count will always contain at least one non-zero value.
• The sum of the values in the count array will never be zero.
Input: Input: [0, 5, 0, 2, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 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]
Explanation: In this example, the dataset has the numbers 1, 2, 3, 4 with respective frequencies: 5, 2, 3, 4. The minimum value is 1, the maximum is 6, the mean is approximately 2.2, the median is 2, and the mode is 1.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus