Sorting:Fraudulent Activity Notifications _bisect.insort() python

59 篇文章 0 订阅
27 篇文章 0 订阅

#!/bin/python3
'''
import math
import os
import random
import re
import sys

# Complete the activityNotifications function below.
#runtime error for some cases
def activityNotifications(expenditure, d):
    count=0
    n=len(expenditure)
    for i in range(n-d):
        trailing=expenditure[i:i+d]
        sort_t=sorted(trailing) #每次循环都要重新sorting,浪费时间
        
        if d%2==0:
            median=(sort_t[d//2]+sort_t[d//2-1])/2
        else:
            median=sort_t[d//2]
        
        if expenditure[i+d]>=2*median:
            count+=1
    return count


if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    nd = input().split()

    n = int(nd[0])

    d = int(nd[1])

    expenditure = list(map(int, input().rstrip().split()))

    result = activityNotifications(expenditure, d)

    fptr.write(str(result) + '\n')

    fptr.close()
'''

#!/bin/python3

import sys
import bisect as bs

def index(a, x):
    'Locate the leftmost value exactly equal to x'
    i = bs.bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    raise ValueError

def median(a_sorted, days):
    half = len(a_sorted)//2
    
    if days % 2:
        median = a_sorted[half]
    else:
        median = (a_sorted[half-1] + a_sorted[half])/2
        
    return float(median)

def activityNotifications(log, days):
    heap = sorted(log[:days])#已经排序
    res = 0
    med = 0
    to_del = 0
    
    for ind in range(days, len(log)):
        med = median(heap, days)
        #print("heap: {}".format(heap))
        #print("log[{}] = {} med = {}".format(ind, log[ind], med))
            
        if float(log[ind]) >= 2*med:
            res += 1
        
        #del heap[heap.index(log[to_del])]
        del heap[index(heap, log[to_del])]#将最左边的删除
        bs.insort(heap, log[ind])#将右边的新的数字加入heap中排序
        to_del += 1
            
    return res
        

if __name__ == "__main__":
    n, d = input().strip().split(' ')
    n, d = [int(n), int(d)]
    expenditure = list(map(int, input().strip().split(' ')))
    result = activityNotifications(expenditure, d)
    print(result)

Another Solution

def median(s):#注意 s is a sorted list
    d=len(s)
    if d%2==0:
        med=(s[d//2]+s[d//2-1])/2
    else:
        med=s[d//2]
    return float(med)

def activityNotifications(expenditure, d):
    heap=sorted(expenditure[:d])
    n=len(expenditure)
    count=0
    to_del=0 
    for i in range(d,n):
        med=median(heap)
        if expenditure[i]>=med*2:
            count+=1
        left=bs.bisect_left(heap,expenditure[to_del])
        del heap[left]
        bs.insort(heap,expenditure[i])
        to_del+=1
    return count

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值