【每日一题20220607】超市排队

:mage:‍ 试着想象一下你在超市买了东西然后排队结账,给定一个数字列表nums和数字n,其中nums中每个数字依次代表等待结账的用户所需分钟数,n表示结账卡口个数。用户优先选择空闲卡口排队结账。 请编写一个函数,计算出需要多久可以让全部用户结账完毕。

【示例】
输入:[10,2,3,3], 2
输出:10
解释:耗时10分钟的用户进入卡口一,卡口持续占用中;耗时2分钟的用户进入卡口二,2分钟后结账完毕,此时耗时3分钟的用户进入空闲的卡口二。以此类推,全部结账完需要10分钟。

题目难度:中等
题目来源:codewars-The Supermarket Queue

def solution(nums: list, n: int)-> int:
    # your code here

assert solution([5,3,4], 1) == 12
assert solution([10,2,3,3], 2) == 10
assert solution([2,3,10], 2) == 12
def solution(team,n):
    a = 0
    b = 0
    if n==1:
        for i in range(len(team)):
            a=a+team[i]
        return a
    else:
        a = team[0]
        b = team[n-1]
        for i in range(2,len(team)):
            if a-b>0:
                b=b+team[i]
            else:
                a=a+team[i]
            if a>b:
                return a
            else:
                return b

assert solution([5,3,4], 1) == 12
assert solution([10,2,3,3], 2) == 10
assert solution([2,3,10], 2) == 12
def solution(nums: list, n: int)-> int:
    nlist = []
    #初始化排队口
    for i in range(n):
        nlist.append(0)
    for num in nums:
        nlist[nlist.index(min(nlist))] += num
    return max(nlist)
1 Like

assert solution([10,2,3,3,12], 2) == 20 这个断言过不去

def solution(nums: list, n: int) -> int:
    # your code here
    queue_list = [0] * n
    if n == 1:
        return sum(nums)
    else:
        for i in nums:
            queue_list[queue_list.index(min(queue_list))] += i
        return max(queue_list)


assert solution([5, 3, 4], 1) == 12
assert solution([10, 2, 3, 3], 2) == 10
assert solution([2, 3, 10], 2) == 12
#果然想复杂了呢哎
def solution(nums: list, n: int)-> int:
    # your code here
    ent = nums[:n]
    T = 0
    for i in nums[n:]:
       minnest = min(ent)
       T += minnest
       for index,value in enumerate(ent):
            ent[index] = value - minnest
            if ent[index] == 0:
                ent[index] += i
    return  T + max(ent)
assert solution([5,3,4], 1) == 12
assert solution([10,2,3,3], 2) == 10
assert solution([2,3,10], 2) == 12
assert solution([10,2,3,3,12], 2) == 20

def solution(nums:list,n:int):
li=[0 for _ in range(n)]
for num in nums:
li[li.index(min(li))]+=num
return max(li)

def solution(nums: list, n: int)-> int:
    if len(nums)==0:return 0
    if 0<len(nums)<=n:
        return max(nums)
    else:
        queue = [nums[i] for i in range(n)]
        queue.sort(reverse=True)
        for i in range(n,len(nums)):
            queue[n-1] +=nums[i]
            #排序
            tmp=queue[n-1]
            left=n-2
            while left>=0 and queue[left]<tmp:
                queue[left + 1] = queue[left]
                left -= 1
            queue[left + 1] = tmp
        return queue[0]
    def solution(nums: list, n: int) -> int:

        # 创建卡口
        lis = [0 for i in range(n)]

        # 时间计算
        for i in nums:
            lis.sort()
            lis[0] += i

        print(lis)

        return max(lis)

    assert solution([5, 3, 4], 1) == 12
    assert solution([10, 2, 3, 3], 2) == 10
    assert solution([2, 3, 10], 2) == 12
def solution(nums: list, n: int)-> int:
        s = 0
        i = n
        while n < len(nums):
            nums_i = nums[0:i]
            nums[0:i] = [j - min(nums_i) for j in nums_i]
            nums[nums_i.index(min(nums_i))] = nums[n]
            s += min(nums_i)
            n += 1
        return s + max(nums)


assert solution([5,3,4], 1) == 12
assert solution([10,2,3,3], 2) == 10
assert solution([2,3,10], 2) == 12
assert solution([3,2,3,6,8,10], 3) == 13
关闭