Skip to content

Commit ff38ed8

Browse files
committed
feat: finish No.2409
1 parent 48819af commit ff38ed8

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# 2409. Count Days Spent Together
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Math, String.
5+
- Similar Questions: Number of Days Between Two Dates, Minimum Number of Operations to Convert Time.
6+
7+
## Problem
8+
9+
Alice and Bob are traveling to Rome for separate business meetings.
10+
11+
You are given 4 strings ```arriveAlice```, ```leaveAlice```, ```arriveBob```, and ```leaveBob```. Alice will be in the city from the dates ```arriveAlice``` to ```leaveAlice``` (**inclusive**), while Bob will be in the city from the dates ```arriveBob``` to ```leaveBob``` (**inclusive**). Each will be a 5-character string in the format ```"MM-DD"```, corresponding to the month and day of the date.
12+
13+
Return** the total number of days that Alice and Bob are in Rome together.**
14+
15+
You can assume that all dates occur in the **same** calendar year, which is **not** a leap year. Note that the number of days per month can be represented as: ```[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]```.
16+
17+
 
18+
Example 1:
19+
20+
```
21+
Input: arriveAlice = "08-15", leaveAlice = "08-18", arriveBob = "08-16", leaveBob = "08-19"
22+
Output: 3
23+
Explanation: Alice will be in Rome from August 15 to August 18. Bob will be in Rome from August 16 to August 19. They are both in Rome together on August 16th, 17th, and 18th, so the answer is 3.
24+
```
25+
26+
Example 2:
27+
28+
```
29+
Input: arriveAlice = "10-01", leaveAlice = "10-31", arriveBob = "11-01", leaveBob = "12-31"
30+
Output: 0
31+
Explanation: There is no day when Alice and Bob are in Rome together, so we return 0.
32+
```
33+
34+
 
35+
**Constraints:**
36+
37+
38+
39+
- All dates are provided in the format ```"MM-DD"```.
40+
41+
- Alice and Bob's arrival dates are **earlier than or equal to** their leaving dates.
42+
43+
- The given dates are valid dates of a **non-leap** year.
44+
45+
46+
47+
## Solution
48+
49+
```javascript
50+
/**
51+
* @param {string} arriveAlice
52+
* @param {string} leaveAlice
53+
* @param {string} arriveBob
54+
* @param {string} leaveBob
55+
* @return {number}
56+
*/
57+
var countDaysTogether = function(arriveAlice, leaveAlice, arriveBob, leaveBob) {
58+
var daysOfMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
59+
var aliceDate = [
60+
arriveAlice.split('-').map(item => Number(item)),
61+
leaveAlice.split('-').map(item => Number(item)),
62+
];
63+
var bobDate = [
64+
arriveBob.split('-').map(item => Number(item)),
65+
leaveBob.split('-').map(item => Number(item)),
66+
];
67+
var togetherDate = [
68+
(aliceDate[0][0] * 100 + aliceDate[0][1]) > (bobDate[0][0] * 100 + bobDate[0][1])
69+
? aliceDate[0]
70+
: bobDate[0],
71+
(aliceDate[1][0] * 100 + aliceDate[1][1]) < (bobDate[1][0] * 100 + bobDate[1][1])
72+
? aliceDate[1]
73+
: bobDate[1],
74+
];
75+
if (togetherDate[0][0] === togetherDate[1][0]) {
76+
return Math.max(togetherDate[1][1] - togetherDate[0][1] + 1, 0);
77+
}
78+
if (togetherDate[0][0] > togetherDate[1][0]) {
79+
return 0;
80+
}
81+
return (daysOfMonth[togetherDate[0][0] - 1] - togetherDate[0][1] + 1)
82+
+ Array(togetherDate[1][0] - togetherDate[0][0] - 1).fill(0)
83+
.map((_, i) => daysOfMonth[i + togetherDate[0][0]])
84+
.reduce((sum, i) => sum + i, 0)
85+
+ togetherDate[1][1];
86+
};
87+
```
88+
89+
**Explain:**
90+
91+
nope.
92+
93+
**Complexity:**
94+
95+
* Time complexity : O(1).
96+
* Space complexity : O(1).

0 commit comments

Comments
 (0)