There is a restaurant with a single chef. You are given an array customers
, where customers[i] = [arrivali, timei]:
arrivali
is the arrival time of the ith
customer. The arrival times are sorted in non-decreasing order.
【资料图】
timei
is the time needed to prepare the order of the ith
customer.
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-5
from 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.
标签:
Thereisarestaurantwithasinglechef Youaregivenanarraycustomers,wherecustomers[i]=[arrivali,timei]:arr
其中,财产险公司总资产2 8万亿元,较年初增长5 1%;人身险公司总...
1、1 可可西里东部与西北部及长江源区的地理、气候条件不同。2、西...
1、古代类:《凤求凰》蓝色连衣裙;《绾青丝》波;《木槿花西月锦绣...
5月19日,全省“百场法治大宣讲”启动仪式暨首场报告会在兰州举行。...
1、百岁爷紫笋茶价格大概在600-2000左右一般礼品盒的要贵些,网上也...
点击上方“蓝字”关注我们吧!6月18日10时至19日10时,榆阳法院将在...
当地时间19日晚,沙特王储穆罕默德·本·萨勒曼在沙特西部城市吉达...
1、首先要知道人民币对日元的汇率,假如1人民币=日元16 8,那么如果...
5月19日,诺安中小盘精选混合最新单位净值为3 038元,累计净值为4...
CFi CN讯:?股东持股的基本情况本次减持计划实施前,北京融通高科...
不知不觉间,中国女排漳州封闭集训终于宣告结束,通过长达三个多月...
可口可乐官宣联动热门游戏《英雄联盟》,推出全新限定口味“英雄登...
数据显示,近三日,出境游机票搜索量环比增长明显,出发时段集中在...
1、辞职申请书尊敬的公司领导:感谢公司这段时间对我的培养,关心和照...
智通财经APP讯天工国际00826发布公告于2023年5月19日该公司斥资2475...
我市一季度工业综合增速位居福建省前三,福建省,三明市,综合增速,城...
许多地方,公积金有新用途了!,首付,商贷,买房,二手房,公积金贷款,...
免费体检办理健康证人员范围1、从事接触直接入口食品工作的生产经营...
今天来聊聊关于兰屿茄十二星瓢虫,十二星瓢虫的文章,现在就为大家...
先土地推介,后挂地拍卖2023年度郑州市首场土地市场推介会举行,郑州...
策划:杨定都统筹:董小娇文案:陈妍言邱自立设计:刘娟魏欣悦
1、李清照(1084年3月13日-1155年),号易安居士,宋齐州章丘(今...
【19~22日多降水气温整体较高】厦门市气象台5月19日11时00分发布未...
去年刚扭亏的龙大美食董事长辞职。5月18日晚间,龙大美食(002726 S...
长沙五险一金缴费标准每年会有一定的调整,而且各个单位上报的工资...
光大同创(301387)05月19日在投资者关系平台上答复了投资者关心的问...
心中有爱,就要告白;爱一个人,就要大声说出来……在“5·20”表白...
作者|麻吉编辑|宋函自今年初中国香港与内地恢复全面通关以来,香港...
象牙梳。 固原博物馆供图宁夏彭阳姚河塬西周城址考古重大发现神秘...