题目:

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!

input

  • customers: an array 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.

output

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


Important

Please look at the examples and clarifications below, to ensure you understand the task correctly :)


Examples

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

Clarifications

  • There is only ONE queue serving many tills, and
  • The order of the queue NEVER changes, and
  • 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.

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

---------------------------------------------------------------------------------------------------------------------------

题目大意:有客户队列customer和收银机器n,你要算出最少的时间

解题方法:

看一下网友的解题算法:

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

解读:根据n的大小造出l的长度,找出l中最小的那个的索引,让此值加i

还有另外一种:

def queue_time(customers, n):
qn = [0] * n
for c in customers:
qn = sorted(qn)
qn[0] += c
return max(qn)

知识点:

1、快速建立一个指定长度的list,L = [0]*n。

2、sort()和sorted()的区别:

  sort()是作用于list上面,是在原来list上进行操作修改,用法是:L.sort()。sorted是作用于一个可迭代对象,返回来的是一个新的对象,用法是:sorted(iterable)。

3、找出某个值所在的索引。使用L.index(n),找出n在L中的索引。

【Kata Daily 190909】The Supermarket Queue(超市队列)的更多相关文章

  1. Queue 先进先出队列的操作

    1.Queue定义 System.Collections.Queue类表示对象的先进先出集合,存储在 Queue(队列) 中的对象在一端插入,从另一端移除. 2.优点 1.能对集合进行顺序处理(先进先 ...

  2. C++数据结构之Queue(队列)

    Queue,队列,和我们日常生活中的队列是同样的规则,"先进先出",从尾入,从首出. Queue,主要有三种基本操作,append(添加元素至队尾):serve(队首元素出列):r ...

  3. python-Day3-set 集合-counter计数器-默认字典(defaultdict) -可命名元组(namedtuple)-有序字典(orderedDict)-双向队列(deque)--Queue单项队列--深浅拷贝---函数参数

    上节内容回顾:C语言为什么比起他语言块,因为C 会把代码变异成机器码Pyhton 的 .pyc文件是什么python 把.py文件编译成的.pyc文件是Python的字节码, 字符串本质是 字符数组, ...

  4. pyhton中的Queue(队列)

    什么是队列? 队列就像是水管子,先进先出,与之相对应的是栈,后进先出. 队列是线程安全的,队列自身有机制可以实现:在同一时刻只有一个线程在对队列进行操作. 存数据,取数据 import Queue q ...

  5. STL --> queue单向队列

    queue单向队列 queue 模板类的定义在<queue>头文件中.与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器 ...

  6. STL - queue(队列)

    Queue简介 queue是队列容器,是一种"先进先出"的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> queu ...

  7. Queue<T>队列与Stack<T>堆栈

    一.概述: Queue<T>队列,对象的先进先出集合("FIFO").Stack<T>栈,对象的后进先出集合("LIFO"). Queu ...

  8. python queue - 同步队列类

    参考 官网 queue 模块 queue 模块实现多生产者,多消费者队列. 当必须在 ==多个线程之间安全地交换信息== 时,它在线程编程中特别有用. 此模块中的Queue类实现了所有必需的锁定语义. ...

  9. SpringBoot项目框架下ThreadPoolExecutor线程池+Queue缓冲队列实现高并发中进行下单业务

    主要是自己在项目中(中小型项目) 有支付下单业务(只是办理VIP,没有涉及到商品库存),目前用户量还没有上来,目前没有出现问题,但是想到如果用户量变大,下单并发量变大,可能会出现一系列的问题,趁着空闲 ...

随机推荐

  1. 数字PLL,什么是数字PLL

    来源:http://www.elecfans.com/baike/bandaoti/bandaotiqijian/20100323203306.html 数字PLL,什么是数字PLL 数字PLL PL ...

  2. DORIS系统概述

    DORIS(Doppler Orbitography and Radio-positioning Integrated by Satellite)(多普勒轨道学与无线电定位集成卫星),它是由法国Cne ...

  3. DES加解密算法(C语言实现)

    DES加密和解密算法的实现(C语言) 主要是做个记录,害怕以后代码丢了,先放到这里了. DES再不进行介绍了,可以看上一篇的 DES 的python实现 转载请注明出处:https://www.cnb ...

  4. shell脚本中,关于if,以及条件判断

    #!/bin/sh SYSTEM=`uname -s` #获取操作系统类型 if [ $SYSTEM = "Linux" ] ; then #如果是linux的话打印linux字符 ...

  5. JVM 第四篇:可视化 JVM 故障处理工具

    本文内容过于硬核,建议有 Java 相关经验人士阅读. 1. 可视化工具 在 JDK 中为我们提供了大量的 JVM 故障处理工具,都在 JDK 的 bin 目录下: 这其中除了大量的命令行工具以外,还 ...

  6. 配置DVWA漏洞环境

    web萌新,因为在别人的环境上练习总有点不舒服,所以在本地搭建了网站:下面记录一下搭建的步骤 DVWA:是一个漏洞环境包,可以用phpstudy或者wamp解析:所以要想配置这个环境,就必须有这两个软 ...

  7. Word+Excel 问题及解决

    [Word] 快捷操作 (1)每个字后面都有换行符的处理办法: 替换:∧p -> 空格 (2)隐藏Word文档中的换行符: word选项 -> 显示 -> 段落标记 [Excel]

  8. MeteoInfoLab脚本示例:Hamawari-8 netCDF data

    示例数据:ftp://ftp.bom.gov.au/anon/sample/catalogue/Satellite/IDE00220.201507140300.nc 该数据的分辨率很高(22000*2 ...

  9. go-zero 如何应对海量定时/延迟任务?

    一个系统中存在着大量的调度任务,同时调度任务存在时间的滞后性,而大量的调度任务如果每一个都使用自己的调度器来管理任务的生命周期的话,浪费cpu的资源而且很低效. 本文来介绍 go-zero 中 延迟操 ...

  10. thinkpad怎么设置u盘启动

    1.按下笔记本的开机键,当屏幕出现"Press F1 for ThinkPad BIOS Setup Utility"提示时,迅速按下f1键,此时系统就自动进入到ThinkPad的 ...