-
Notifications
You must be signed in to change notification settings - Fork 42
/
solution.js
29 lines (27 loc) · 811 Bytes
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* @param {string} arriveAlice
* @param {string} leaveAlice
* @param {string} arriveBob
* @param {string} leaveBob
* @return {number}
*/
var countDaysTogether = function (arriveAlice, leaveAlice, arriveBob, leaveBob) {
const daysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, ];
const daysBeforeMonth = [0, ];
for (let i = 0; i < daysPerMonth.length - 1; i++) {
daysBeforeMonth[i + 1] = daysBeforeMonth[i] + daysPerMonth[i];
}
const parse = (s) => {
const [m, d, ] = s.split('-');
return daysBeforeMonth[+m - 1] + (+d);
};
const start = Math.max(
parse(arriveAlice),
parse(arriveBob)
);
const end = Math.min(
parse(leaveAlice),
parse(leaveBob)
);
return Math.max(0, end - start + 1);
};