Skip to content

Commit 27efce1

Browse files
Merge branch 'main' of https://github.com/contextlab/leetcode-solutions into main
2 parents 83304e2 + 2b90d97 commit 27efce1

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

problems/1701/jeremymanning.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# [Problem 1701: Average Waiting Time](https://leetcode.com/problems/average-waiting-time/description/?envType=daily-question)
2+
3+
## Initial thoughts (stream-of-consciousness)
4+
- Given the length of the customer list, I think we'll need to find an $O(n)$ solution
5+
- Things to track:
6+
- Total wait time so far
7+
- Number of customers (this is just `len(customers)`, so we don't actually need to "track" it per se...but still)
8+
- Current time that the chef is occupied until
9+
- Any special cases? 🤔...
10+
- What if all of the arrival times are the same? --> Then the average wait time is just `sum([c[1] for c in customers]) / len(customers)`
11+
- Arrival times are already sorted, so no need to rearrange the list...
12+
13+
## Refining the problem, round 2 thoughts
14+
- Let's start with `totalWait = 0` and `chefBusyUntil = 0`
15+
- With each new customer (`[x, y]`):
16+
- If `x < chefBusyUntil`:
17+
- `totalWait += chefBusyUntil - x` # wait until the chef is done with the current workload
18+
- Else:
19+
- `chefBusyUntil = x` # need to wait until the next customer arrives to start on their request
20+
- `totalWait += y`
21+
- `chefBusyUntil += y`
22+
- Then return `totalWait / len(customers)`
23+
24+
## Attempted solution(s)
25+
```python
26+
class Solution:
27+
def averageWaitingTime(self, customers: List[List[int]]) -> float:
28+
totalWait = 0
29+
chefBusyUntil = 0
30+
31+
for x, y in customers:
32+
if x < chefBusyUntil:
33+
totalWait += chefBusyUntil - x
34+
else:
35+
chefBusyUntil = x
36+
totalWait += y
37+
chefBusyUntil += y
38+
39+
return totalWait / len(customers)
40+
```
41+
- given test cases pass
42+
- `customers = [[5,2],[5,4],[10,3],[20,1],[21,10000],[21, 5]]`: pass
43+
- `customers = [[1, 1000]]`: pass
44+
- Ok, this seems fine...submitting...
45+
46+
![Screenshot 2024-07-08 at 10 43 28 PM](https://github.com/ContextLab/leetcode-solutions/assets/9030494/d5bcb9a8-1e6d-4867-9158-2a7d68f2b796)
47+
48+
solved!

0 commit comments

Comments
 (0)