题目:

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中的索引。

最新文章

  1. 意法STM32F1系列MCU单片机解密芯片破解复制
  2. Yii2的邮件配置
  3. 从头开始编写一个Orchard网上商店模块(1) - 介绍
  4. URAL 1306 - Sequence Median 小内存求中位数
  5. TODO、FIXME和XXX转载
  6. TCP粘包的拆包处理
  7. (原)vs2013静态及动态链接opencv3.0的库
  8. JavaScript瀑布流代码
  9. 属性动画(Property Animation)
  10. Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{...
  11. js 工厂模式、简单模式、抽象模式
  12. js JQuery 获取元素和遍历
  13. spring boot引入json,jsonobject,需要指定jdk15
  14. soapUI-Webservice接口测试
  15. TortoiseSVN常用配置
  16. PCI 设备调试手段
  17. scrapy框架之分布式操作
  18. Mac下如何安装WebStorm + 破解
  19. [JSOI 2007]字符加密Cipher
  20. 第四章 TCP粘包/拆包问题的解决之道---4.2--- 未考虑TCP粘包导致功能异常案例

热门文章

  1. 计数,dic的创建方式,求九九乘法表
  2. macvlan几种模式
  3. 多测师讲解性能测试_面试题_001高级讲师肖sir
  4. MeteoInfoLab脚本示例:站点数据绘制等值线
  5. java中文件是否为空
  6. day49 Pyhton 数据库Mysql 06
  7. C语言编程丨循环链表实现约瑟夫环!真可谓无所不能的C!
  8. lumen-ioc容器测试 (6)
  9. 如何获取前端提交来得json格式数据
  10. centos8平台上php7.4的生产环境配置