最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

LeetCode 1701. Average Waiting Time

2023-05-20 13:13 作者:您是打尖兒還是住店呢  | 我要投稿

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

其實(shí)就是放兩個(gè)變量,1個(gè)變量是每次等待的時(shí)間,另一個(gè)變量是每次做飯完成的時(shí)間點(diǎn),

每次去判斷完成的時(shí)間點(diǎn)跟客人到達(dá)的時(shí)間去比較;

完成的時(shí)間超過(guò)客人到達(dá)的時(shí)間,那么就要多等一會(huì)了,多等的時(shí)間是完成的時(shí)間減去客戶到達(dá)的時(shí)間。

如果完成的時(shí)間小于客人到達(dá)的時(shí)間,那么就是廚師多等一會(huì)了,客人多等的時(shí)間就是廚師做飯的時(shí)間了。

下面是代碼:

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的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
神农架林区| 十堰市| 石阡县| 阿拉善盟| 剑川县| 房山区| 余干县| 上蔡县| 翁牛特旗| 舟山市| 吉木萨尔县| 陇川县| 新余市| 沽源县| 富蕴县| 金寨县| 宁晋县| 依兰县| 长汀县| 兖州市| 南郑县| 青冈县| 库车县| 定州市| 潞城市| 枣阳市| 甘孜县| 色达县| 封开县| 六枝特区| 米脂县| 宁陕县| 衡东县| 杭锦旗| 云林县| 阿图什市| 缙云县| 龙陵县| 广饶县| 庄浪县| 沈丘县|