题目:

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. 【转载】绕过CDN找到源站的思路

    [原文:https://mp.weixin.qq.com/s/8NUvPqEzVjO3XbmCBukUvQ] 绕过CDN的思路 网上有很多绕过CDN的思路,但是存在很多问题,以下是收集并总结的思路.站 ...

  2. Java 使用UDP传输一个小文本文件

    工具1:Eclipse 工具2:IntelliJ IDEA Java工程的目录结构(基于IntelliJ IDEA) 例1.1:接收方,因为接收到的数据是字节流,为了方便,这里是基于Apache co ...

  3. 【优化】单调队列与dp

    笔者大概看了一下单调队列对于DP的优化,故撰此文,望有帮助. (dp还是推式子难啊qwq) 例题1. 题目大意:在n个数的序列中,选择数字,使得其连续不超过k个数,且和最大. 本题的方程相对好推:设d ...

  4. iPhone手机越狱-逆向砸壳-代码注入

    iPhone手机越狱 逆向砸壳 代码注入 工具下载 操作越狱 安装待逆向应用(app) 使用OpenSSH连接手机 找到应用二进制文件地址 找到应用document沙盒地址 拷贝砸壳工具(dumpde ...

  5. 用< 100行代码向EPUB或Web服务器添加视频回放

    下载source - 32.3 KB 下载latest version from GituHub 介绍 在我 在关于CodeProject的前一篇文章中,我展示了一个简单的EPUB查看器 Androi ...

  6. 如何快速构建React组件库

    前言 俗话说:"麻雀虽小,五脏俱全",搭建一个组件库,知之非难,行之不易,涉及到的技术方方面面,犹如海面风平浪静,实则暗礁险滩,处处惊险- 目前团队内已经有较为成熟的 Vue 技术 ...

  7. K8S基础

    四组基本概念 Pod/Pod控制器 Name/Namespace Label/Label选择器 Service/Ingress Pod Pod是k8s里能够被运行的最小的逻辑单元(原子单元) 1个Po ...

  8. CDH5部署三部曲之二:部署和设置

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  9. 多测师讲解jmeter _图片详解_(全)高级讲师肖sir

  10. python面试题-django相关

    1.中间件 中间件一般做认证或批量请求处理,django中的中间件,其实是一个类,在请求和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法, 如请求过来 执行process_re ...