阿里笔试3.30第一题(python)

没有参加笔试,优先队列实现的,测试用例能通过,大家帮忙看看有没有问题
import heapq
import math
class jichang():
    def __init__(self, index, num):
        self.index = index
        self.num = num

    def __lt__(self, other):
        return self.num > other.num
        # return self.num < other.num

    def __eq__(self, other):
        return self.num == other.num


def count(n, arr, k, m):
    heap = []
    initial = sum(arr)
    total_sold = 0
    for i in range(n):
        heapq.heappush(heap, jichang(i+1, arr[i]))

    for i in range(m):
        for element in heap: 
            element.num += k

        Max = heapq.heappop(heap)
        sold = math.ceil(Max.num / 2)
        heapq.heappush(heap, jichang(Max.index, Max.num - sold))
        total_sold += sold
        Max.num -= sold

    return initial + m*k*n - total_sold

#阿里巴巴##笔试题目#
全部评论

相关推荐

点赞 1 评论
分享
牛客网
牛客企业服务