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

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1378
Problem TLDR
Min finish time of land + water single events
Intuition
Brute-force: take every water even and compare with every land event Optimal: 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
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
fun earliestFinishTime(lst: IntArray, ld: IntArray, wst: IntArray, wd: IntArray): Int {
val f = { s:IntArray,d:IntArray,m:Int -> s.indices.minOf{max(m,s[it])+d[it]}}
return min(f(wst,wd, f(lst,ld,0)),f(lst,ld,f(wst,wd,0)))
}
pub fn earliest_finish_time(lst: Vec<i32>, ld: Vec<i32>, wst: Vec<i32>, wd: Vec<i32>) -> i32 {
let f = |s: &[i32], d: &[i32], m: i32| (0..s.len()).map(|i| s[i].max(m) + d[i]).min().unwrap();
f(&wst, &wd, f(&lst, &ld, 0)).min(f(&lst, &ld, f(&wst, &wd, 0)))
}
Comments