题目:

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. Burp时间到期之复活

    Burp昨天到期了,找了好久终于找到可以用的了,分享给大家. https://pan.baidu.com/s/1hsEhUYS r6ls PS:https://github.com/mxcxvn/Bu ...

  2. vue项目的elementui的form表单label的对齐方式和 el-date-picker 的长度设置

    1.先按照官网的  :label-position  属性玩了一下毫无效果:发现单独使用这个属性是无效的,必须和  label-width 属性一起使用才生效: 如: <el-form :mod ...

  3. 机器学习算法——kNN(k-近邻算法)

    算法概述 通过测量不同特征值之间的距离进行 [分类] 优点:精度高.对异常值不敏感.无数据输入假定. 缺点:计算复杂度高.空间复杂度高. 适用数据范围: 数值型 和 标称型 . 算法流程 数据 样本数 ...

  4. SpringBoot多任务Quartz动态管理Scheduler,时间配置,页面+源码

    页面展现 后台任务处理:恢复任务 15s执行一次后台打印消息 不BB了,直接上代码 import... /** * 调度工厂类 * Created by jinyu on 2018/4/14/014. ...

  5. 跨境 TCP 传输优化实录 — 使用 BBR 解决 LFN 问题

    背景 近期开通了一条访问美国机房的 1G 专线,用于提供行情数据备源,并基于 TCP 建立了一套数据传输服务.上线后发现一个严重的问题:应用程序发送队列中的数据大量积压,最终导致程序 OOM Kill ...

  6. 【C语言C++编程学习笔记】一种很酷的 C 语言技巧,灵活运用编程技巧让你写代码事半功倍!

    C语言常常让人觉得它所能表达的东西非常有限.它不具有类似第一级函数和模式匹配这样的高级功能.但是C非常简单,并且仍然有一些非常有用的语法技巧和功能,只是没有多少人知道罢了. ☆ 指定的初始化 很多人都 ...

  7. k8s-命令创建service

    查看命令帮助 [root@master kubernetes]# kubectl create service -h Create a service using specified subcomma ...

  8. RESP协议

    RESP 是 Redis 序列化协议的简写.它是⼀种直观的⽂本协议,优势在于实现异常简单,解析性能极好. Redis 协议将传输的结构数据分为 5 种最⼩单元类型,单元结束时统⼀加上回⻋换⾏符号\r\ ...

  9. centos8安装fastdfs6.06集群方式三之:storage的安装/配置/运行

    一,查看本地centos的版本 [root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) 说 ...

  10. goland 注册

    注意:本教程已过期  请使用其他人教程激活最新版   https://www.789zhao.com/blog/JC08EIFBS9TM.html https://shimo.im/docs/dKYC ...