/**
* Problem: LeetCode 2409 - Count Days Spent Together
* Difficulty: Easy
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int countDaysTogether(String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) {
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int[] prefix = new int[13];
for (int m = 1; m <= 12; m++) {
prefix[m] = prefix[m - 1] + days[m - 1];
}
int aA = toDay(arriveAlice, prefix);
int lA = toDay(leaveAlice, prefix);
int aB = toDay(arriveBob, prefix);
int lB = toDay(leaveBob, prefix);
int start = Math.max(aA, aB);
int end = Math.min(lA, lB);
return Math.max(0, end - start + 1);
}
private int toDay(String date, int[] prefix) {
int month = Integer.parseInt(date.substring(0, 2));
int day = Integer.parseInt(date.substring(3, 5));
return prefix[month - 1] + day;
}
}
"""
Problem: LeetCode 2409 - Count Days Spent Together
Difficulty: Easy
Language: Python
Author: CodeByTushu
"""
class Solution:
def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int:
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
prefix = [0] * 13
for m in range(1, 13):
prefix[m] = prefix[m - 1] + days[m - 1]
def to_day(date: str) -> int:
month = int(date[:2])
day = int(date[3:5])
return prefix[month - 1] + day
a_a = to_day(arriveAlice)
l_a = to_day(leaveAlice)
a_b = to_day(arriveBob)
l_b = to_day(leaveBob)
start = max(a_a, a_b)
end = min(l_a, l_b)
return max(0, end - start + 1)
/**
* Problem: LeetCode 2409 - Count Days Spent Together
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int countDaysTogether(string arriveAlice, string leaveAlice, string arriveBob, string leaveBob) {
vector<int> days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
vector<int> prefix(13, 0);
for (int m = 1; m <= 12; m++) {
prefix[m] = prefix[m - 1] + days[m - 1];
}
auto toDay = [&](const string& date) {
int month = stoi(date.substr(0, 2));
int day = stoi(date.substr(3, 2));
return prefix[month - 1] + day;
};
int aA = toDay(arriveAlice);
int lA = toDay(leaveAlice);
int aB = toDay(arriveBob);
int lB = toDay(leaveBob);
int start = max(aA, aB);
int end = min(lA, lB);
return max(0, end - start + 1);
}
};
/**
* Problem: LeetCode 2409 - Count Days Spent Together
* Difficulty: Easy
* Language: JavaScript
* Author: CodeByTushu
*/
var countDaysTogether = function(arriveAlice, leaveAlice, arriveBob, leaveBob) {
const days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const prefix = new Array(13).fill(0);
for (let m = 1; m <= 12; m++) {
prefix[m] = prefix[m - 1] + days[m - 1];
}
const toDay = (date) => {
const month = parseInt(date.substring(0, 2), 10);
const day = parseInt(date.substring(3, 5), 10);
return prefix[month - 1] + day;
};
const aA = toDay(arriveAlice);
const lA = toDay(leaveAlice);
const aB = toDay(arriveBob);
const lB = toDay(leaveBob);
const start = Math.max(aA, aB);
const end = Math.min(lA, lB);
return Math.max(0, end - start + 1);
};