• 【leetcode】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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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.

    解题思路:最大值,最小值,平均数和众数都很简单。中位数我是用两次循环求的,第一次求出样本的个数,根据个数求出中位数的下标,然后第二次循环即可得到。

    代码如下:

    class Solution(object):
        def sampleStats(self, count):
            """
            :type count: List[int]
            :rtype: List[float]
            """
            minv = None
            maxv = 0
            times = sum(count)
            amount = 0
            most = 0
            mostCount = 0
            inx1 = times / 2
            if times % 2 == 0:
                inx2 = inx1 - 1
            else:
                inx2 = None
    
            median1 = 0
            median2 = None
    
            tmpTimes = 0
            for i in range(len(count)):
                if count[i] != 0 and minv == None:
                    minv = i
                if count[i] != 0:
                    maxv = i
                if count[i] != 0:
                    if count[i] > mostCount:
                        most = i
                        mostCount = count[i]
                if inx1 >= tmpTimes and inx1 <= tmpTimes + count[i]:
                    median1 = i
                if inx2 != None and inx2 >= tmpTimes and inx2 <= tmpTimes + count[i]:
                    median2 = i
                tmpTimes += count[i]
    
                amount += (count[i]*i)
            median = median1 if inx2 == None  else (float(median2) + float(median1)) / float(2)
    
            return [float(minv),float(maxv),float(amount)/float(times),float(median),float(most)]
  • 相关阅读:
    bitcoin PoW原理及区块创建过程
    Hyperledger Fabric(v1.1.0)编译时遇到的问题
    Hyperledger Fabic中的Transaction流程
    mint linux 18.3 遇到“已安装的 post-installation 脚本 返回了错误号 127 ”问题的解决
    redis--解析字符串
    golang 统计uint64 数字二进制存储中1的数量
    c++ std 最小堆的使用 (用于实现top100之类的功能)
    Linux 信号signal处理函数
    Linux 信号signal处理机制
    LinuxMint 下 B站 番 blv 缓存 转 mp4
  • 原文地址:https://www.cnblogs.com/seyjs/p/11075472.html
Copyright © 2020-2023  润新知