Alice and Bob are traveling to Rome for separate business meetings.
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.
Return the total number of days that Alice and Bob are in Rome together.
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].
Input: arriveAlice ="08-15", leaveAlice ="08-18", arriveBob ="08-16", leaveBob ="08-19"Output: 3Explanation: 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 is3.
Input: arriveAlice ="10-01", leaveAlice ="10-31", arriveBob ="11-01", leaveBob ="12-31"Output: 0Explanation: There is no day when Alice and Bob are in Rome together, so we return0.
The days Alice and Bob spend together are the overlapping days between their two date intervals. We can convert the dates to day-of-year numbers and compute the overlap.
classSolution {
public:int countDaysTogether(string aA, string lA, string aB, string lB) {
vector<int> days = {0,31,59,90,120,151,181,212,243,273,304,334,365};
auto f = [&](string d) {
int m = stoi(d.substr(0,2)), dd = stoi(d.substr(3,2));
return days[m-1] + dd;
};
int s = max(f(aA), f(aB)), e = min(f(lA), f(lB));
returnmax(0, e - s +1);
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
funccountDaysTogether(aA, lA, aB, lBstring) int {
days:= []int{0,31,59,90,120,151,181,212,243,273,304,334,365}
f:=func(dstring) int {
m:= (int(d[0]-'0')*10+ int(d[1]-'0'))
dd:= (int(d[3]-'0')*10+ int(d[4]-'0'))
returndays[m-1] +dd }
s:= max(f(aA), f(aB))
e:= min(f(lA), f(lB))
ifs > e { return0 }
returne-s+1}
func max(a, bint) int { ifa > b { returna }; returnb }
func min(a, bint) int { ifa < b { returna }; returnb }
1
2
3
4
5
6
7
8
9
10
11
classSolution {
publicintcountDaysTogether(String aA, String lA, String aB, String lB) {
int[] days = {0,31,59,90,120,151,181,212,243,273,304,334,365};
java.util.function.Function<String, Integer> f = d -> {
int m = Integer.parseInt(d.substring(0,2)), dd = Integer.parseInt(d.substring(3,5));
return days[m-1]+ dd;
};
int s = Math.max(f.apply(aA), f.apply(aB)), e = Math.min(f.apply(lA), f.apply(lB));
return Math.max(0, e - s + 1);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
funcountDaysTogether(aA: String, lA: String, aB: String, lB: String): Int {
val days = listOf(0,31,59,90,120,151,181,212,243,273,304,334,365)
funf(d: String): Int {
val m = d.substring(0,2).toInt()
val dd = d.substring(3,5).toInt()
return days[m-1] + dd
}
val s = maxOf(f(aA), f(aB))
val e = minOf(f(lA), f(lB))
returnif (s > e) 0else e - s + 1 }
}
1
2
3
4
5
6
7
8
9
classSolution:
defcountDaysTogether(self, aA: str, lA: str, aB: str, lB: str) -> int:
days = [0,31,59,90,120,151,181,212,243,273,304,334,365]
deff(d: str) -> int:
m, dd = int(d[:2]), int(d[3:])
return days[m-1] + dd
s = max(f(aA), f(aB))
e = min(f(lA), f(lB))
return max(0, e - s +1)
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfncount_days_together(a_a: String, l_a: String, a_b: String, l_b: String) -> i32 {
let days = [0,31,59,90,120,151,181,212,243,273,304,334,365];
let f =|d: &str| {
let m = d[0..2].parse::<usize>().unwrap();
let dd = d[3..5].parse::<usize>().unwrap();
days[m-1] + dd
};
let s = f(&a_a).max(f(&a_b));
let e = f(&l_a).min(f(&l_b));
if s > e { 0 } else { (e - s +1) asi32 }
}
}