Statistics from a Large Sample

LintCode Q 1093 - Statistics from a Large Sample

We sampled integers between 0 and 255, and stored the results in an array count: count[k] is the number of integers we sampled equal to k.
Return the minimum, maximum, mean, median, and mode of the sample respectively, as an array of floating point numbers. The mode is guaranteed to be unique.
(Recall that the median of a sample is: The middle element, if the elements of the sample were sorted and the number of elements is odd;
The average of the middle two elements, if the elements of the sample were sorted and the number of elements is even.)

Example 1: Input: count = [0,1,3,4] ; Output: [1.00000,3.00000,2.375,2.50000,3.00000]

Constraints:

  • count.length == 256
  • 1 <= sum(count) <= 10^9
  • The mode of the sample that count represents is unique.
  • Answers within 10^-5 of the true value will be accepted as correct.

Solution

Note: the type of sum should be long, not int.

Code:

public double[] sampleStats(int[] count) {

  int[] res = new int[5];

  int minNumber = -1, maxNumber = 0, mode = 0, totalNumber = 0;
  long sum = 0;

  for (int i = 0; i < count.length; i++) {
    if (count[i] != 0) {
      if (minNumber == -1) minNumber = i;
        maxNumber = i;
        if (count[i] > count[mode]) mode = i;
        totalNumber += count[i];
        sum += i * count[i]; 
    }
  }

  res[0] = minNumber; res[1] = maxNumber; res[2] = sum / (double) totalNumber; res[4] = mode;

  int midIndex = totalNumber % 2 == 1 ? totalNumber / 2 + 1 : totalNumber / 2;
  int count = 0;

  for (int i = 0; i < count.length; i++) {
    if (count[i] != 0) {
      curNumber += count[i];
      if (curNumber > medianIndex1) {
        res[3] = i; break;
      } else if (curNumber == medianIndex1) {
        if (totalNumber % 2 == 1) {
          res[3] = i; break;
        } else {
          int j = i + 1;
          while (j < count.length) {
            if (count[j] != 0) break;
            j++;
          }
          res[3] = (i + j) / 2.0;
          break;
        }
      }
    }        
  }

  return res;
}

   Reprint policy


《Statistics from a Large Sample》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
  TOC