codewars(python)练习笔记十:计算超市排队时长

codewars(python)练习笔记十:计算超市排队时长

题目

There is a queue for the self-checkout tills at the supermarket. Your task is write a function to calculate the total time required for all the customers to check out!

The function has two input variables:

  • customers: an array (list in python) of positive integers representing the queue. Each integer represents a customer, and its value is the amount of time they require to check out.
  • n: a positive integer, the number of checkout tills.

The function should return an integer, the total time required.

EDIT: A lot of people have been confused in the comments. To try to prevent any more confusion:

  • There is only ONE queue, and
  • The order of the queue NEVER changes, and
  • Assume that the front person in the queue (i.e. the first element in the array/list) proceeds to a till as soon as it becomes free.
  • The diagram on the wiki page I linked to at the bottom of the description may be useful.

So, for example:

queue_time([5,3,4], 1)
# should return 12
# because when n=1, the total time is just the sum of the times

queue_time([10,2,3,3], 2)
# should return 10
# because here n=2 and the 2nd, 3rd, and 4th people in the 
# queue finish before the 1st person has finished.

queue_time([2,3,10], 2)
# should return 12

N.B. You should assume that all the test input will be valid, as specified above.

P.S. The situation in this kata can be likened to the more-computer-science-related idea of a thread pool, with relation to running multiple processes at the same time: https://en.wikipedia.org/wiki/Thread_pool

Test case:
queue_time([2,3,10], 2) should be 12
queue_time([], 5) should be 0
queue_time([2], 5) should be 2
queue_time([1,2,3,4,5], 100) should be 5
queue_time([2,3,10,2,3], 2) should be 12

题目大意:

这道题是经典的超市购物排队问题:有一个队列 customers[],队列中每个元素是当前顾客的处理时长,有 n 个购物台,可以同时处理n个客户。这是题目大意,求的是队列的处理时长。
特别注意:

  • 只有一个队列,
  • 队列处理顺序不能变,
  • 假设队列中的前面的人(即数组/列表中的第一个元素)在有购物台空闲的时候就会继续前进。

我的解法:

#!/usr/bin/python

def queue_time(customers, n):
    if customers == []:
        return 0
    if len(customers) < n:
        return max(customers)
    list_temp = [customers[i] for i in range(0,n)]
    for item in range(n,len(customers)):
        list_temp[list_temp.index(min(list_temp))] += item
    return max(list_temp)

牛逼解法:

#!/usr/bin/python

def queue_time(customers, n):
    list_temp = [0]*n
    for item in customers:
        list_temp[list_temp.index(min(list_temp))] += item
    return max(list_temp)

我跟最牛逼的解法之间的差距在于:我意识到了 list_temp 通过 [customers[i] for i in range(0,n)]这样创建,绝对不是最佳解法。但没有想到,可以直接生成长度为n 的空数组。

另一个牛逼解法:

def queue_time(customers, n):
    queues = [0] * n
    for i in customers:
        queues.sort()
        queues[0] += i
    return max(queues)

相对于上一个,sort() 要比min() 算法复杂度高一些。

另一个牛逼解法:

def queue_time(cs, n):
    def serve(q,c): return sorted([q[0]+c] + q[1:])
    return max(reduce(serve, cs, [0]*n))

利用reduce(),相当精奇的思路。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,475评论 4 372
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,744评论 2 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,101评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,732评论 0 221
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,141评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,049评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,188评论 2 320
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,965评论 0 213
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,716评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,867评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,341评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,663评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,376评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,200评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,990评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,179评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,979评论 2 279

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,129评论 0 10
  • 说不丑的 恭喜你没有吃饭照镜子的坏毛病 EAT和FAT之间,只有一柜之隔。 我可以做静静瞻仰食物的萌物 但低下头呵...
    七幕人生音乐剧阅读 701评论 0 2
  • 丝路春风两千年 渭水古渡富长安 文人墨客多眷恋 一带一路大西咸 西咸
    雷金昌阅读 992评论 0 1
  • 今天同事寒寒小美女做了一个表格,给我工作上受到一个小启发,慧琳美女给我推荐了一个好物-蚊帐,所谓三人行,必有我师焉...
    兰莹格格阅读 125评论 0 0