-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion_32.cpp
More file actions
27 lines (26 loc) · 737 Bytes
/
Question_32.cpp
File metadata and controls
27 lines (26 loc) · 737 Bytes
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
int md[13] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
class Solution {
// Convert date to respective number in year.
int td(string s) {
int m = s[1] - '0' + 10*(s[0] - '0');
int d = s[4] - '0' + 10*(s[3] - '0');
--m;
while (m > 0) {
d += md[m];
--m;
}
return d;
}
public:
int countDaysTogether(string sa1, string sa2, string sb1, string sb2) {
int a1 = td(sa1), a2 = td(sa2), b1 = td(sb1), b2 = td(sb2);
int ans = 0;
// Check all 365 days in year
for (int i = 0; i < 367; ++i) {
if (a1 <= i && i <= a2 && b1 <= i && i <= b2) {
++ans;
}
}
return ans;
}
};