其他分享
首页 > 其他分享> > leetcode1093

leetcode1093

作者:互联网

这道题,我看了半天没看懂,等看懂的时候,时间都快结束了。

class Solution:
    def sampleStats(self, count: 'List[int]') -> 'List[float]':
        l = []
        sums = 0
        n = 0
        maxv = -1
        minv = 256
        modev = -1
        modetime = 0
        for i in range(len(count)):
            c = count[i]
            if c != 0:
                l.append((i,c))
                sums += c * i
                n += c
                maxv = max(maxv,i)
                minv = min(minv,i)
                if modetime < c:
                    modetime = c
                    modev = i
        meanv = sums / n
        medianv = 0
        half = 0
        if n % 2 == 1:
            for i in range(len(l)):
                half += l[i][1]
                if half >= n // 2:
                    medianv = l[i][0]
                    break
        else:
            for i in range(len(l)):
                half += l[i][1]
                if half == n // 2:
                    medianv = (l[i][0] + l[i+1][0]) / 2
                    break
                elif half > n // 2:
                    medianv = l[i][0]
                    break
        return [float(minv),float(maxv),float(meanv),float(medianv),float(modev)]

 

标签:modetime,float,maxv,medianv,half,leetcode1093,minv
来源: https://www.cnblogs.com/asenyang/p/11072239.html