Alice 和 Bob 计划分别去罗马开会。
给你四个字符串 arriveAlice
,leaveAlice
,arriveBob
和 leaveBob
。Alice 会在日期 arriveAlice
到 leaveAlice
之间在城市里(日期为闭区间),而 Bob 在日期 arriveBob
到 leaveBob
之间在城市里(日期为闭区间)。每个字符串都包含 5 个字符,格式为 "MM-DD"
,对应着一个日期的月和日。
请你返回 Alice和 Bob 同时在罗马的天数。
你可以假设所有日期都在 同一个 自然年,而且 不是 闰年。每个月份的天数分别为:[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
。
示例 1:
输入:arriveAlice = "08-15", leaveAlice = "08-18", arriveBob = "08-16", leaveBob = "08-19" 输出:3 解释:Alice 从 8 月 15 号到 8 月 18 号在罗马。Bob 从 8 月 16 号到 8 月 19 号在罗马,他们同时在罗马的日期为 8 月 16、17 和 18 号。所以答案为 3 。
示例 2:
输入:arriveAlice = "10-01", leaveAlice = "10-31", arriveBob = "11-01", leaveBob = "12-31" 输出:0 解释:Alice 和 Bob 没有同时在罗马的日子,所以我们返回 0 。
class Solution:
def date_to_day_of_year(self, date_str: str) -> int:
month, day = map(int, date_str.split('-'))
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
return sum(days_in_month[:month-1]) + day
def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:
alice_start = self.date_to_day_of_year(arriveAlice)
alice_end = self.date_to_day_of_year(leaveAlice)
bob_start = self.date_to_day_of_year(arriveBob)
bob_end = self.date_to_day_of_year(leaveBob)
overlap_start = max(alice_start, bob_start)
overlap_end = min(alice_end, bob_end)
return max(0, overlap_end - overlap_start + 1)