|
| 1 | +/** |
| 2 | + * 1093. Statistics from a Large Sample |
| 3 | + * https://leetcode.com/problems/statistics-from-a-large-sample/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * You are given a large sample of integers in the range [0, 255]. Since the sample is so large, |
| 7 | + * it is represented by an array count where count[k] is the number of times that k appears in |
| 8 | + * the sample. |
| 9 | + * |
| 10 | + * Calculate the following statistics: |
| 11 | + * - minimum: The minimum element in the sample. |
| 12 | + * - maximum: The maximum element in the sample. |
| 13 | + * - mean: The average of the sample, calculated as the total sum of all elements divided by the |
| 14 | + * total number of elements. |
| 15 | + * - median: |
| 16 | + * - If the sample has an odd number of elements, then the median is the middle element once |
| 17 | + * the sample is sorted. |
| 18 | + * - If the sample has an even number of elements, then the median is the average of the two |
| 19 | + * middle elements once the sample is sorted. |
| 20 | + * - mode: The number that appears the most in the sample. It is guaranteed to be unique. |
| 21 | + * |
| 22 | + * Return the statistics of the sample as an array of floating-point numbers [minimum, maximum, |
| 23 | + * mean, median, mode]. Answers within 10-5 of the actual answer will be accepted. |
| 24 | + */ |
| 25 | + |
| 26 | +/** |
| 27 | + * @param {number[]} count |
| 28 | + * @return {number[]} |
| 29 | + */ |
| 30 | +var sampleStats = function(count) { |
| 31 | + let minimum = 256; |
| 32 | + let maximum = -1; |
| 33 | + let sum = 0; |
| 34 | + let totalCount = 0; |
| 35 | + let mode = 0; |
| 36 | + let maxFrequency = 0; |
| 37 | + |
| 38 | + for (let value = 0; value < 256; value++) { |
| 39 | + const frequency = count[value]; |
| 40 | + if (frequency > 0) { |
| 41 | + minimum = Math.min(minimum, value); |
| 42 | + maximum = Math.max(maximum, value); |
| 43 | + sum += value * frequency; |
| 44 | + totalCount += frequency; |
| 45 | + if (frequency > maxFrequency) { |
| 46 | + maxFrequency = frequency; |
| 47 | + mode = value; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + const mean = sum / totalCount; |
| 53 | + const median = findMedian(count, totalCount); |
| 54 | + |
| 55 | + return [minimum, maximum, mean, median, mode]; |
| 56 | +}; |
| 57 | + |
| 58 | +function findMedian(count, totalCount) { |
| 59 | + const isOdd = totalCount % 2 === 1; |
| 60 | + const target = Math.floor(totalCount / 2); |
| 61 | + let currentCount = 0; |
| 62 | + let firstMedian = -1; |
| 63 | + |
| 64 | + for (let value = 0; value < 256; value++) { |
| 65 | + currentCount += count[value]; |
| 66 | + if (isOdd) { |
| 67 | + if (currentCount > target) return value; |
| 68 | + } else { |
| 69 | + if (firstMedian === -1 && currentCount >= target) { |
| 70 | + firstMedian = value; |
| 71 | + } |
| 72 | + if (currentCount > target) { |
| 73 | + return (firstMedian + value) / 2; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments