LeetCode Entry
2391. Minimum Amount of Time to Collect Garbage
Time to pick 3-typed garbage[] by 3 trucks traveling to the right travel[] time
2391. Minimum Amount of Time to Collect Garbage medium
blog post
substack

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/411
Problem TLDR
Time to pick 3-typed garbage[] by 3 trucks traveling to the right travel[] time
Intuition
We can hardcode the algorithm from the description examples, for each truck individually.
Approach
Let’s try to minify the code:
- all garbage must be picked up, so add
garbage.sumBy { it.length } - for each type, truck will travel until the last index with this type
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
fun garbageCollection(garbage: Array<String>, travel: IntArray): Int =
garbage.sumBy { it.length } +
"MPG".sumBy { c ->
(1..garbage.indexOfLast { c in it }).sumBy { travel[it - 1] }
}