LeetCode Entry
3635. Earliest Finish Time for Land and Water Rides II
Min finish time of land + water single events
3635. Earliest Finish Time for Land and Water Rides II medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1379
Problem TLDR
Min finish time of land + water single events
Intuition
find min water and land finish times, then try all waters with finish land and try all lands with finish water
Approach
- extract the repeating parts
- Rust can derive the argument types in lambdas
- Kotlin: we can extract sub-sub functions
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
fun earliestFinishTime(lst: IntArray, ld: IntArray, wst: IntArray, wd: IntArray)= run {
fun f(st: IntArray, d: IntArray) = {s: Int ->st.zip(d).minOf{(st,d)->max(s,st)+d}}
val (a,b) = f(lst, ld) to f(wst, wd); min(a(b(0)), b(a(0)))
}
pub fn earliest_finish_time(lst: Vec<i32>, ld: Vec<i32>, wst: Vec<i32>, wd: Vec<i32>) -> i32 {
let f = |st:&[i32],d:&[i32], s| (0..d.len()).map(|i|st[i].max(s)+d[i]).min().unwrap();
f(&lst, &ld, f(&wst, &wd, 0)).min(f(&wst, &wd, f(&lst, &ld, 0)))
}
Comments