Leetcode 2409: Count Days Spent Together

grid47
grid47
Exploring patterns and algorithms
Mar 11, 2024 7 min read

Alice and Bob are traveling to Rome for separate business meetings. You are given four strings representing their travel dates: arriveAlice, leaveAlice, arriveBob, and leaveBob. Each string follows the format ‘MM-DD’, which represents the month and day of their respective travel dates. Alice will be in Rome from ‘arriveAlice’ to ’leaveAlice’, while Bob will be in the city from ‘arriveBob’ to ’leaveBob’. Your task is to calculate how many days they will both be in Rome together.
Problem
Approach
Steps
Complexity
Input: The input consists of four strings: arriveAlice, leaveAlice, arriveBob, and leaveBob. Each string represents a date in the format 'MM-DD'.
Example: arriveAlice = '07-10', leaveAlice = '07-15', arriveBob = '07-12', leaveBob = '07-17'
Constraints:
• 1 <= month <= 12
• 1 <= day <= 31
• Alice's arrival date is earlier than or equal to their departure date.
• Bob's arrival date is earlier than or equal to their departure date.
• The dates are valid within a non-leap year.
Output: Return an integer representing the number of days Alice and Bob will both be in Rome together.
Example: Output: 4
Constraints:
Goal: The goal is to find the overlap between the two time intervals (one for Alice and one for Bob). The number of days they both spend in Rome together is the length of the intersection of these two intervals.
Steps:
• 1. Convert the string dates into a day of the year format.
• 2. Find the overlapping range by calculating the maximum of the start dates and the minimum of the end dates.
• 3. If there is an overlap, calculate the number of overlapping days by subtracting the start date of the overlap from the end date.
• 4. Return the total number of overlapping days.
Goal: The solution should efficiently handle all valid date ranges for a non-leap year.
Steps:
• Time complexity should be O(1), as the operations are based on fixed date conversions.
• Space complexity should also be O(1), since we are only using a few variables.
Assumptions:
• The dates provided are valid and fall within the same calendar year.
• Alice and Bob may or may not have overlapping travel dates.
Input: arriveAlice = '07-10', leaveAlice = '07-15', arriveBob = '07-12', leaveBob = '07-17'
Explanation: In this example, Alice is in Rome from July 10 to July 15, and Bob is in Rome from July 12 to July 17. They will both be in Rome together from July 12 to July 15, which gives 4 days of overlap.

Input: arriveAlice = '10-01', leaveAlice = '10-31', arriveBob = '11-01', leaveBob = '12-31'
Explanation: In this example, Alice is in Rome in October, and Bob is in Rome from November onwards. There are no overlapping days, so the result is 0.

Input: arriveAlice = '02-15', leaveAlice = '02-18', arriveBob = '02-16', leaveBob = '02-20'
Explanation: Alice is in Rome from February 15 to February 18, and Bob is in Rome from February 16 to February 20. They overlap from February 16 to February 18, so the result is 3 days.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus