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 }
  • 相关阅读:
    从尾到头打印链表
    在链表结尾插入一个结点 以及在 在链表中找到第一个含有某值的结点并删除该结点
    替换空格
    二维数组中的查找
    简单选择排序
    冒泡排序
    Hash表的实现
    二叉排序树
    ajax返回后台编译时都对,返回error
    sql删除重复的记录保留一条
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11404219.html
Copyright © 2011-2022 走看看