# 1093. Statistics from a Large Sample¶

• Time:
• Space:
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 class Solution { public: vector sampleStats(vector& count) { const int n = accumulate(count.begin(), count.end(), 0); const double mode = ranges::max_element(count) - count.begin(); return { getMinimum(count), getMaximum(count), getMean(count, n), (getLeftMedian(count, n) + getRightMedian(count, n)) / 2.0, mode, }; } private: double getMinimum(const vector& count) { for (int i = 0; i < count.size(); ++i) if (count[i]) return i; return -1; } double getMaximum(const vector& count) { for (int i = count.size() - 1; i >= 0; --i) if (count[i]) return i; return -1; } double getMean(const vector& count, double n) { double mean = 0; for (long i = 0; i < count.size(); ++i) mean += (i * count[i]) / n; return mean; } double getLeftMedian(const vector& count, double n) { int numCount = 0; for (int i = 0; i < count.size(); ++i) { numCount += count[i]; if (numCount >= n / 2) return i; } return -1; } double getRightMedian(const vector& count, double n) { int numCount = 0; for (int i = count.size() - 1; i >= 0; --i) { numCount += count[i]; if (numCount >= n / 2) return i; } return -1; } }; 
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 class Solution { public double[] sampleStats(int[] count) { final int n = Arrays.stream(count).sum(); return new double[] { getMinimum(count), // getMaximum(count), // getMean(count, n), // (getLeftMedian(count, n) + getRightMedian(count, n)) / 2.0, // getMode(count), }; } private double getMinimum(int[] count) { for (int i = 0; i < count.length; ++i) if (count[i] > 0) return i; return -1; } private double getMaximum(int[] count) { for (int i = count.length - 1; i >= 0; --i) if (count[i] > 0) return i; return -1; } private double getMean(int[] count, double n) { double mean = 0; for (int i = 0; i < count.length; ++i) mean += ((long) i * (long) count[i]) / n; return mean; } private double getLeftMedian(int[] count, double n) { int numCount = 0; for (int i = 0; i < count.length; ++i) { numCount += count[i]; if (numCount >= n / 2) return i; } return -1; } private double getRightMedian(int[] count, double n) { int numCount = 0; for (int i = count.length - 1; i >= 0; --i) { numCount += count[i]; if (numCount >= n / 2) return i; } return -1; } private double getMode(int[] count) { int mode = -1; int maxCount = 0; for (int i = 0; i < count.length; ++i) if (count[i] > maxCount) { maxCount = count[i]; mode = i; } return mode; } } 
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 class Solution: def sampleStats(self, count: List[int]) -> List[float]: minimum = next((i for i, num in enumerate(count) if num), None) maximum = next((i for i, num in reversed( list(enumerate(count))) if num), None) n = sum(count) mean = sum(i * c / n for i, c in enumerate(count)) mode = count.index(max(count)) numCount = 0 leftMedian = 0 for i, c in enumerate(count): numCount += c if numCount >= n / 2: leftMedian = i break numCount = 0 rightMedian = 0 for i, c in reversed(list(enumerate(count))): numCount += c if numCount >= n / 2: rightMedian = i break return [minimum, maximum, mean, (leftMedian + rightMedian) / 2, mode]