Count Days Spent Together - Problem
Count Days Spent Together - A Date Intersection Problem
Alice and Bob are traveling to Rome for separate business meetings. You need to find how many days they'll be in the city at the same time.
Given four date strings in
โข
โข
Goal: Return the total number of days both Alice and Bob are in Rome together (inclusive of arrival and departure dates).
Important: All dates are in the same non-leap year. Month lengths: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Example: If Alice is there "08-15" to "08-18" and Bob is there "08-16" to "08-19", they overlap on "08-16", "08-17", "08-18" = 3 days.
Alice and Bob are traveling to Rome for separate business meetings. You need to find how many days they'll be in the city at the same time.
Given four date strings in
"MM-DD" format:โข
arriveAlice & leaveAlice - Alice's arrival and departure datesโข
arriveBob & leaveBob - Bob's arrival and departure datesGoal: Return the total number of days both Alice and Bob are in Rome together (inclusive of arrival and departure dates).
Important: All dates are in the same non-leap year. Month lengths: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Example: If Alice is there "08-15" to "08-18" and Bob is there "08-16" to "08-19", they overlap on "08-16", "08-17", "08-18" = 3 days.
Input & Output
example_1.py โ Basic Overlap
$
Input:
arriveAlice = "08-15", leaveAlice = "08-18", arriveBob = "08-16", leaveBob = "08-19"
โบ
Output:
3
๐ก Note:
Alice is in Rome from Aug 15-18, Bob is there from Aug 16-19. They overlap on Aug 16, 17, and 18 = 3 days total.
example_2.py โ No Overlap
$
Input:
arriveAlice = "10-01", leaveAlice = "10-31", arriveBob = "11-01", leaveBob = "12-31"
โบ
Output:
0
๐ก Note:
Alice leaves on Oct 31 and Bob arrives on Nov 1. There are no overlapping days.
example_3.py โ Complete Overlap
$
Input:
arriveAlice = "09-01", leaveAlice = "10-19", arriveBob = "09-04", leaveBob = "09-09"
โบ
Output:
6
๐ก Note:
Bob's entire stay (Sep 4-9) falls within Alice's stay (Sep 1 - Oct 19). Bob is there for 6 days total.
Constraints
- All dates are valid and in the format MM-DD
- All dates occur in the same non-leap year
- 1 โค MM โค 12 and 1 โค DD โค DaysInMonth[MM]
- arriveAlice โค leaveAlice and arriveBob โค leaveBob
- Month lengths: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Visualization
Tap to expand
Understanding the Visualization
1
Parse Dates
Convert MM-DD strings to day numbers (Jan 1 = Day 1, Dec 31 = Day 365)
2
Map Ranges
Visualize Alice's and Bob's stays as ranges on a timeline
3
Find Intersection
The overlap starts at the later arrival and ends at the earlier departure
4
Count Days
Calculate days = end - start + 1, or 0 if no overlap exists
Key Takeaway
๐ฏ Key Insight: Date overlap is just range intersection math - convert to numbers first, then use max/min operations to find the overlapping period efficiently in O(1) time!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code