LeetCode solutions

2496. Count Days Spent Together

My accepted Python solution to LeetCode problem 2496, Count Days Spent Together, running in 0ms.

  • Difficulty: Easy
  • Python
  • Runtime 0ms
  • Memory 17.3MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 17.3MB, accepted 2025-12-29.

python
class Solution:
    def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:
        days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        
        def to_day_number(date):
            month, day = int(date[:2]), int(date[3:])
            return sum(days_in_month[:month-1]) + day
        
        alice_start = to_day_number(arriveAlice)
        alice_end = to_day_number(leaveAlice)
        bob_start = to_day_number(arriveBob)
        bob_end = to_day_number(leaveBob)
        
        overlap_start = max(alice_start, bob_start)
        overlap_end = min(alice_end, bob_end)
        
        return max(0, overlap_end - overlap_start + 1)

Source