LeetCode Entry
1701. Average Waiting Time
Average of intersecting intervals
1701. Average Waiting Time medium
blog post
substack
youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/664
Problem TLDR
Average of intersecting intervals #medium #simulation
Intuition
Just simulate the process.
Approach
Let’s use iterators to save lines of code.
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
fun averageWaitingTime(customers: Array<IntArray>): Double {
var time = 0
return customers.sumOf { (start, delta) ->
time = max(start, time) + delta
(time - start).toDouble()
} / customers.size
}
pub fn average_waiting_time(customers: Vec<Vec<i32>>) -> f64 {
let mut time = 0;
customers.iter().map(|c| {
time = time.max(c[0]) + c[1];
(time - c[0]) as f64
}).sum::<f64>() / customers.len() as f64
}