zoukankan      html  css  js  c++  java
  • LeetCode 1093. Statistics from a Large Sample

    原题链接在这里:https://leetcode.com/problems/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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    Output: [1.00000,3.00000,2.37500,2.50000,3.00000]
    

    Example 2:

    Input: 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]
    Output: [1.00000,4.00000,2.18182,2.00000,1.00000] 

    Constraints:

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

    题解:

    The count is the frequency of selected numbers.

    mode is the number with highest frequency.

    First iteration, calculate totoal number selected countSum.

    If countSum is even, median should be between (countSum+1)/2 and (countSum+2)/2.

    If countSum is odd, median should (countSum+1)/2. Here (countSum+2)/2 equals to (countSum+1)/2.

    Inorder to make code consistent, it doesn't need to separate into odd and even cases, just take half from both indices.

    Time Complexity: O(n). n = count.length. Two iterations.

    Space: O(1).

    AC Java:

     1 class Solution {
     2     public double[] sampleStats(int[] count) {
     3         double min = -1;
     4         double max = 0;
     5         int countSum = 0;
     6         double sum = 0;
     7         double maxCount = 0;
     8         double mode = 0;
     9         for(int i = 0; i<count.length; i++){
    10             if(min==-1 && count[i]!=0){
    11                 min = i;
    12             }
    13             
    14             if(count[i] != 0){
    15                 max = i;
    16             }
    17             
    18             countSum += count[i];
    19             sum += count[i]*i*1.0;
    20             if(maxCount < count[i]){
    21                 maxCount = count[i];
    22                 mode = i;
    23             }
    24         }
    25         
    26         double median = 0.0;
    27         int m1 = (countSum+1)/2;
    28         int m2 = (countSum+2)/2;
    29         int countNum = 0;
    30         for(int i = 0; i<count.length; i++){
    31             if(countNum<m1 && countNum+count[i]>=m1){
    32                 median += i/2.0;
    33             }
    34             
    35             if(countNum<m2 && countNum+count[i]>=m2){
    36                 median += i/2.0;
    37             }
    38             
    39             countNum+=count[i];
    40         }
    41         
    42         return new double[]{min, max, sum/countSum, median, mode};
    43     }
    44 }
  • 相关阅读:
    【转】Oracle中的decode在mysql中的等价实现
    Perhaps you are running on a JRE rather than a JDK
    iisapp -a命令出现 :此脚本不能与WScript工作
    HDU 6070 线段树
    HDU 1853 MCMF
    Codeforces 7C 扩展欧几里得
    HDU 5675 智慧数
    Lucas 大组合数
    bzoj 2179 FFT
    POJ 1155 树形背包
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11404219.html
Copyright © 2011-2022 走看看