LeetCode 1701. Average Waiting Time 全球快消息

2023-05-20 13:14:27 来源:哔哩哔哩 分享到:

There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrivali, timei]:

arrivaliis the arrival time of the ithcustomer. The arrival times are sorted in non-decreasing order.

timeiis the time needed to prepare the order of the ithcustomer.


(资料图)

When a customer arrives, he gives the chef his order, and the chef starts preparing it once he is idle. The customer waits till the chef finishes preparing his order. The chef does not prepare food for more than one customer at a time. The chef prepares food for customers in the order they were given in the input.

Return the average waiting time of all customers. Solutions within 10-5from the actual answer are considered accepted.

Example 1:

Input: customers = [[1,2],[2,5],[4,3]]

Output: 5.00000

Explanation:

1) The first customer arrives at time 1, the chef takes his order and starts preparing it immediately at time 1, and finishes at time 3, so the waiting time of the first customer is 3 - 1 = 2. 

2) The second customer arrives at time 2, the chef takes his order and starts preparing it at time 3, and finishes at time 8, so the waiting time of the second customer is 8 - 2 = 6. 

3) The third customer arrives at time 4, the chef takes his order and starts preparing it at time 8, and finishes at time 11, so the waiting time of the third customer is 11 - 4 = 7.So the average waiting time = (2 + 6 + 7) / 3 = 5.

Example 2:

Input: customers = [[5,2],[5,4],[10,3],[20,1]]Output: 3.25000

Explanation:

1) The first customer arrives at time 5, the chef takes his order and starts preparing it immediately at time 5, and finishes at time 7, so the waiting time of the first customer is 7 - 5 = 2. 

2) The second customer arrives at time 5, the chef takes his order and starts preparing it at time 7, and finishes at time 11, so the waiting time of the second customer is 11 - 5 = 6. 

3) The third customer arrives at time 10, the chef takes his order and starts preparing it at time 11, and finishes at time 14, so the waiting time of the third customer is 14 - 10 = 4. 

4) The fourth customer arrives at time 20, the chef takes his order and starts preparing it immediately at time 20, and finishes at time 21, so the waiting time of the fourth customer is 21 - 20 = 1.So the average waiting time = (2 + 6 + 4 + 1) / 4 = 3.25.

Constraints:

1 <= customers.length <= 105

1 <= arrivali, timei <= 104

arrivali <= arrivali+1

其实就是放两个变量,1个变量是每次等待的时间,另一个变量是每次做饭完成的时间点,

每次去判断完成的时间点跟客人到达的时间去比较;

完成的时间超过客人到达的时间,那么就要多等一会了,多等的时间是完成的时间减去客户到达的时间。

如果完成的时间小于客人到达的时间,那么就是厨师多等一会了,客人多等的时间就是厨师做饭的时间了。

下面是代码:

Runtime: 3 ms, faster than 75.19% of Java online submissions for Average Waiting Time.

Memory Usage: 87.1 MB, less than 94.74% of Java online submissions for Average Waiting Time.

标签:

LeetCode 1701. Average Waiting Time 全球快消息

来源:哔哩哔哩 2023-05-20 13:14:27

重点聚焦!购买二手房的税费怎么算_购买二手房的税费

来源:互联网 2023-05-20 12:08:31

世界观点:《云襄传》:武侠尚在,江湖未曾远离

来源:哔哩哔哩 2023-05-20 11:05:35

【热点】证实了,火星北部曾存在海洋

来源:吉林之声 2023-05-20 10:18:14

南模生物:5月19日融资买入13.94万元,融资融券余额4581.1万元-全球速看料

来源:证券之星 2023-05-20 09:12:28

詹姆斯季后赛末节命中率34.6%,三分命中率5%_速讯

来源:手机网易网 2023-05-20 08:29:15

降速的元气森林如何自救?开始“讨好”经销商 播报

来源:亚汇网 2023-05-20 07:24:07

“偶发”高增长后 体外诊断企业业绩波动大

来源:中国经营网 2023-05-20 06:25:23

微软690亿美元收购暴雪又一个障碍扫除 魔兽国服重开有望了?

来源:快科技 2023-05-20 05:07:46

5月19日基金净值:银华盛利混合发起式A最新净值2.5377,跌0.41%|今日精选

来源:证券之星 2023-05-20 02:46:10

TrendForce集邦:2023年全球服务器整机出货量将下修至1383.5万台 同比减少2.85%

来源:凤凰网 2023-05-20 00:08:26

哈萨克斯坦总统:愿进一步扩大哈中双边贸易等各领域合作 环球热资讯

来源:海口网  2023-05-19 22:07:04

环球新消息丨重庆宽带资费对比上海宽度(重庆宽带资费对比)

来源:互联网 2023-05-19 21:12:30

【全球速看料】小区物业擅自启用电梯管控系统,业主起诉要求停用胜诉

来源:扬子晚报 2023-05-19 20:52:27

三境界人生三境界_三境界_资讯

来源:互联网 2023-05-19 19:46:46

未来1000天,不拥抱大模型的人会被边缘化 天天时讯

来源:i黑马 2023-05-19 18:57:31

中国人民银行:中国人民银行对2022年度公开市场业务一级交易商及申请加入一级交易商的机构进行了综合评估,根据评估结果,确定了2023年度公开市场业务一级交易商。此外,为配合民营企业债券融资支持工具,再次将中债信用增进投资股份有限公司纳入公开市场业务一级交易商。(中国人民银行)|今日看点

来源:汇通财经 2023-05-19 18:28:32

天天快消息!高铁霸座女子个人信息_高铁霸座女

来源:互联网 2023-05-19 18:00:44

开普检测(003008.SZ):目前珠海基地的各项业务还在有序开展之中,订单总量尚不能覆盖成本费用 环球观焦点

来源:格隆汇 2023-05-19 17:12:48

焦点要闻:硬笔草书谁的好_硬笔草书

来源:互联网 2023-05-19 16:58:46

新消息丨神州租车开放2023年端午租车预订窗口

来源:DoNews快讯 2023-05-19 16:25:32

南财快评:年报成绩单已交,退市公司“新一波”下的A股新风貌 天天讯息

来源:21世纪经济报道 2023-05-19 15:50:45

世界微速讯:TMT行业私募创投市场将有望重新展现出投资活力

来源:新民晚报 2023-05-19 15:15:46

又以"越界"为由!台当局查扣大陆渔船 5人被扣押_今日热讯

来源:看看新闻 2023-05-19 14:30:35

失聪女被认定恶势力头目_被控女孩称“倩家帮”是QQ群群名

来源:互联网 2023-05-19 14:08:47

AI狂飙时代,如何扼住技术的缰绳-天天微动态

来源:经济观察网 2023-05-19 13:19:20

武神觉醒 战至巅峰《镇魂街:武神觉醒》公测今日开启!

来源:游民星空[整理] 2023-05-19 12:39:20

注会报名如何修改照片_注会报名照片能改 全球热文

来源:互联网 2023-05-19 11:46:51

极速摩托飞车什么时候出 公测上线时间预告|全球报道

来源:九游 2023-05-19 11:35:19

女生甜美网名_女生QQ网名甜美型的有哪些

来源:互联网 2023-05-19 10:52:33

Copyright   2015-2022 华东知识产权网 版权所有  备案号:京ICP备2022016840号-41   联系邮箱:2 913 236 @qq.com