Juni_Dev_log

(LeetCode) Average Waiting Time with Python 본문

CodingTest/인프런 (Algorithm)

(LeetCode) Average Waiting Time with Python

Juni_K 2021. 3. 2. 11:40

Average Waiting Time

 

📌 시간복잡도 

O(N) : for문으로 동작

📌 공간복잡도 

O(1)

 

 

Problem

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

(한 명의 요리사가 있는 레스토랑이 있다.  customers[i] = [arrivali, timei] 인 고객배열로 제공된다.)

 

  • arrivali is the arrival time of the ith customer. The arrival times are sorted in non-decreasing order.
  • (arrivali는 i번째 고객의 도착 시간이다. 도착 시간은 감소하지 않는 순서로 정렬된다.)
  • timei is the time needed to prepare the order of the ith customer.
  • (timei는 i번째 고객의 주문을 준비하는데 필요한 시간이다.)

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.

(모든 고객의 평균 대기 시간을 반환한다. 실제 답변에서 10-5이내의 해결책은 허용된 것으로 간주된다.)

 


 

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.

(첫 번째 고객은 1시에 도착하고 요리사는 주문을 받아서 1시에 즉시 준비를 시작하고 3시에 완료하므로 첫 번째 고객의 대기 시간은 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.

(두 번째 고객은 2시에 도착하고 요리사는 주문을 받아 3시에 준비를 시작하고 8시에 완료하므로 두 번째 고객의 대기 시간은 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.

(세 번째 고객은 4시에 도착하고 요리사는 주문을 받아 8시에 준비를 시작하고 세 번째 고객의 대기 시간은 11-4=이다. 따라서 평균 대기 시간 = (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.

(첫 번째 고객은 5시에 도착하고 요리사는 주문을 받아 5시에 즉시 준비를 시작하고, 7시에 완료되므로 첫 번째 고객의 대기시간은 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.

(두 번째 고객은 5시에 도착하고 요리사는 주문을 받아 7시에 준비를 시작하고 11시에 완료되기 때문에, 두 번째 고객의 대기 시간은 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.

(세 번째 고객은 10시에 도착하고 요리사는 주문을 받아 11시에 준비를 시작하고 14시에 완료하므로 세 번째 고객의 대기 시간은 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.

(네 번째 고객은 20시에 도착하고 요리사는 주문을 받아 20시에 즉시 준비를 시작하고 21시에 완료하므로 네 번째 고객의 대기시간은 21-20=1이다. 따라서, 평균 대기 시간 = (2+6+4+1)/4 = 3.25 이다.)

 


Constraints:

  • 1 <= customers.length <= 105
  • 1 <= arrivali, timei <= 104
  • arrivali <= arrivali+1

 

💯 Solution

 

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution(object):
    def averageWaitingTime(self, customers):
        """
        :type customers: List[List[int]]
        :rtype: float
        """
        waiting_time = 0
        cooked = 0
        for arr, time in customers:
            cooked = max(arr, cooked) + time
            waiting_time += cooked - arr
            
        return waiting_time/float(len(customers))
cs

 

 

 

📚 참고

github.com/JiHoon-JK/Algorithm/blob/main/LeetCode/medium/Average%20Waiting%20Time.py

 

JiHoon-JK/Algorithm

알고리즘 공부를 위한 저장소입니다. Contribute to JiHoon-JK/Algorithm development by creating an account on GitHub.

github.com

 

Comments