LeetCode Entry
2409. Count Days Spent Together
Days range intersection
2409. Count Days Spent Together easy substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1521
Problem TLDR
Days range intersection
Intuition
Convert to day of a year
Approach
- we can compare strings directly
Complexity
-
Time complexity: \(O(1)\)
-
Space complexity: \(O(1)\)
Code
fun countDaysTogether(aa: String, la: String, ab: String, lb: String): Int {
val days = listOf(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
fun d(s: String) = days.take(s.take(2).toInt()).sum() + s.drop(3).toInt()
return max(0, d(minOf(la, lb)) - d(maxOf(aa, ab)) + 1)
}
pub fn count_days_together(aa: String, la: String, ab: String, lb: String) -> i32 {
let d = |s: &str| [0,31,59,90,120,151,181,212,243,273,304,334][s[..2].parse::<usize>().unwrap() - 1] + s[3..].parse::<i32>().unwrap();
(d(&la.min(lb)) - d(&aa.max(ab)) + 1).max(0)
}
Comments