java.lang.Object
g1001_1100.s1093_statistics_from_a_large_sample.Solution

public class Solution extends Object
1093 - Statistics from a Large Sample.<p>Medium</p> <p>You are given a large sample of integers in the range <code>[0, 255]</code>. Since the sample is so large, it is represented by an array <code>count</code> where <code>count[k]</code> is the <strong>number of times</strong> that <code>k</code> appears in the sample.</p> <p>Calculate the following statistics:</p> <ul> <li><code>minimum</code>: The minimum element in the sample.</li> <li><code>maximum</code>: The maximum element in the sample.</li> <li><code>mean</code>: The average of the sample, calculated as the total sum of all elements divided by the total number of elements.</li> <li><code>median</code>: <ul> <li>If the sample has an odd number of elements, then the <code>median</code> is the middle element once the sample is sorted.</li> <li>If the sample has an even number of elements, then the <code>median</code> is the average of the two middle elements once the sample is sorted.</li> </ul> </li> <li><code>mode</code>: The number that appears the most in the sample. It is guaranteed to be <strong>unique</strong>.</li> </ul> <p>Return <em>the statistics of the sample as an array of floating-point numbers</em> <code>[minimum, maximum, mean, median, mode]</code><em>. Answers within</em> <code>10<sup>-5</sup></code> <em>of the actual answer will be accepted.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> 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]</p> <p><strong>Output:</strong> [1.00000,3.00000,2.37500,2.50000,3.00000]</p> <p><strong>Explanation:</strong> The sample represented by count is [1,2,2,2,3,3,3,3].</p> <p>The minimum and maximum are 1 and 3 respectively.</p> <p>The mean is (1+2+2+2+3+3+3+3) / 8 = 19 / 8 = 2.375.</p> <p>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.</p> <p>The mode is 3 as it appears the most in the sample.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> 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]</p> <p><strong>Output:</strong> [1.00000,4.00000,2.18182,2.00000,1.00000]</p> <p><strong>Explanation:</strong> The sample represented by count is [1,1,1,1,2,2,2,3,3,4,4].</p> <p>The minimum and maximum are 1 and 4 respectively.</p> <p>The mean is (1+1+1+1+2+2+2+3+3+4+4) / 11 = 24 / 11 = 2.18181818&hellip; (for display purposes, the output shows the rounded number 2.18182).</p> <p>Since the size of the sample is odd, the median is the middle element 2.</p> <p>The mode is 1 as it appears the most in the sample.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>count.length == 256</code></li> <li><code>0 <= count[i] <= 10<sup>9</sup></code></li> <li><code>1 <= sum(count) <= 10<sup>9</sup></code></li> <li>The mode of the sample that <code>count</code> represents is <strong>unique</strong>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • sampleStats

      public double[] sampleStats(int[] count)