Skip to content

Commit 3b15802

Browse files
my solution for problem 1701
1 parent 02c3164 commit 3b15802

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

problems/1701/jeremymanning.md

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,48 @@
11
# [Problem 1701: Average Waiting Time](https://leetcode.com/problems/average-waiting-time/description/?envType=daily-question)
22

33
## 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...
412

513
## 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)`
623

724
## Attempted solution(s)
825
```python
9-
class Solution: # paste your code here!
10-
...
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)
1140
```
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)