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]
.
Example 1:
Input: arriveAlice = "08-15 ", leaveAlice = "08-18 ", arriveBob = "08-16 ", leaveBob = "08-19 " Output: 3 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.
Example 2:
Input: arriveAlice = "10-01 ", leaveAlice = "10-31 ", arriveBob = "11-01 ", leaveBob = "12-31 " Output: 0 Explanation: There is no day when Alice and Bob are in Rome together, so we return 0.
Constraints:
"MM-DD "
.program main
implicit none
character(len=5) :: arriveAlice, leaveAlice, arriveBob, leaveBob
integer :: numDays
! Example 1
arriveAlice = '08-15 '
leaveAlice = '08-18 '
arriveBob = '08-16 '
leaveBob = '08-19 '
write(*,*) solve(arriveAlice, leaveAlice, arriveBob, leaveBob)
! Example 2
arriveAlice = '10-01 '
leaveAlice = '10-31 '
arriveBob = '11-01 '
leaveBob = '12-31 '
write(*,*) solve(arriveAlice, leaveAlice, arriveBob, leaveBob)
contains
function solve(arriveAlice, leaveAlice, arriveBob, leaveBob) result(numDays)
implicit none
character(len=5), intent(in) :: arriveAlice, leaveAlice, arriveBob, leaveBob
integer :: numDays
integer, parameter :: DAYS_PER_MONTH(12) = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
integer :: i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
i = index(arriveAlice, '-')
j = index(leaveAlice, '-')
k = index(arriveBob, '-')
l = index(leaveBob, '-')
m = 0
do while (m < 12)
if (i == 3 .and. j == 3) then
if (arriveAlice(1:2) == arriveBob(1:2)) then
if (arriveAlice(4:5) <= arriveBob(4:5)) then
if (leaveAlice(4:5) >= leaveBob(4:5)) then
m = m + DAYS_PER_MONTH(m)
end if
end if
end if
else if (k == 3 .and. l == 3) then
if (arriveAlice(1:2) == arriveBob(1:2)) then
if (arriveAlice(4:5) <= arriveBob(4:5)) then
if (leaveAlice(4:5) >= leaveBob(4:5)) then
m = m + DAYS_PER_MONTH(m)
end if
end if
end if
end if
i = i + 1
j = j + 1
k = k + 1
l = l + 1
m = m + 1
end do
numDays = m - 1
end function solve
end program main
11 11
program aliceAndBob
implicit none
integer :: i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
integer :: daysAlice, daysBob, daysTogether
character(len=5) :: arriveAlice, leaveAlice, arriveBob, leaveBob
read(*,*) arriveAlice, leaveAlice, arriveBob, leaveBob
daysAlice = 0
daysBob = 0
daysTogether = 0
do i = 1, 12
do j = 1, 31
if (arriveAlice(1:2) == i) then
if (arriveAlice(4:5) == j) then
daysAlice = daysAlice + 1
end if
end if
if (leaveAlice(1:2) == i) then
if (leaveAlice(4:5) == j) then
daysAlice = daysAlice - 1
end if
end if
if (arriveBob(1:2) == i) then
if (arriveBob(4:5) == j) then
daysBob = daysBob + 1
end if
end if
if (leaveBob(1:2) == i) then
if (leaveBob(4:5) == j) then
daysBob = daysBob - 1
end if
end if
end do
end do
do i = 1, 12
do j = 1, 31
if (daysAlice == 0 .and. daysBob == 0) then
exit
end if
if (daysAlice > 0 .and. daysBob > 0) then
if (arriveAlice(1:2) == i) then
if (arriveAlice(4:5) == j) then
if (daysAlice == 1) then
daysAlice = 0
else
daysAlice = daysAlice - 1
end if
end if
end if
if (arriveBob(1:2) == i) then
if (arriveBob(4:5) == j) then
if (daysBob == 1) then
daysBob = 0
else
daysBob = daysBob - 1
end if
end if
end if
if (daysAlice > 0 .and. daysBob > 0) then
daysTogether = daysTogether + 1
end if
end if
end do
end do
write(*,*) daysTogether
end program aliceAndBob
temp.f95:16:14: 16 | if (arriveAlice(1:2) == i) then | 1 Error: Operands of comparison operator ‘==’ at (1) are CHARACTER(*)/INTEGER(4) temp.f95:17:16: 17 | if (arriveAlice(4:5) == j) then | 1 Error: Operands of comparison operator ‘==’ at (1) are CHARACTER(*)/INTEGER(4) temp.f95:21:14: 21 | if (leaveAlice(1:2) == i) then | 1 Error: Operands of comparison operator ‘==’ at (1) are CHARACTER(*)/INTEGER(4) temp.f95:22:16: 22 | if (leaveAlice(4:5) == j) then | 1 Error: Operands of comparison operator ‘==’ at (1) are CHARACTER(*)/INTEGER(4) temp.f95:26:14: 26 | if (arriveBob(1:2) == i) then | 1 Error: Operands of comparison operator ‘==’ at (1) are CHARACTER(*)/INTEGER(4) temp.f95:27:16: 27 | if (arriveBob(4:5) == j) then | 1 Error: Operands of comparison operator ‘==’ at (1) are CHARACTER(*)/INTEGER(4) temp.f95:31:14: 31 | if (leaveBob(1:2) == i) then | 1 Error: Operands of comparison operator ‘==’ at (1) are CHARACTER(*)/INTEGER(4) temp.f95:32:16: 32 | if (leaveBob(4:5) == j) then | 1 Error: Operands of comparison operator ‘==’ at (1) are CHARACTER(*)/INTEGER(4) temp.f95:45:16: 45 | if (arriveAlice(1:2) == i) then | 1 Error: Operands of comparison operator ‘==’ at (1) are CHARACTER(*)/INTEGER(4) temp.f95:46:18: 46 | if (arriveAlice(4:5) == j) then | 1 Error: Operands of comparison operator ‘==’ at (1) are CHARACTER(*)/INTEGER(4) temp.f95:54:16: 54 | if (arriveBob(1:2) == i) then | 1 Error: Operands of comparison operator ‘==’ at (1) are CHARACTER(*)/INTEGER(4) temp.f95:55:18: 55 | if (arriveBob(4:5) == j) then | 1 Error: Operands of comparison operator ‘==’ at (1) are CHARACTER(*)/INTEGER(4)
def days_together(arrive_alice, leave_alice, arrive_bob, leave_bob):
days_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def date_to_days(date):
month, day = map(int, date.split('-'))
return sum(days_in_month[:month]) + day
arrive_days_alice = date_to_days(arrive_alice)
leave_days_alice = date_to_days(leave_alice)
arrive_days_bob = date_to_days(arrive_bob)
leave_days_bob = date_to_days(leave_bob)
common_start_date = max(arrive_days_alice, arrive_days_bob)
common_end_date = min(leave_days_alice, leave_days_bob)
return max(0, common_end_date - common_start_date + 1)
daysInMonth
.dateToDays
that takes a date string and the daysInMonth
array, then converts the date string to the total number of days from the beginning of the year. Calculate this by summing up days from prior months plus the day of the month.arriveAlice
, leaveAlice
, arriveBob
, and leaveBob
by calling dateToDays
.arriveDaysAlice
and arriveDaysBob
.leaveDaysAlice
and leaveDaysBob
.#include <string>
#include <algorithm>
int daysTogether(const std::string& arriveAlice, const std::string& leaveAlice, const std::string& arriveBob, const std::string& leaveBob) {
int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
auto dateToDays = [&](const std::string& date) {
int month = stoi(date.substr(0, 2));
int day = stoi(date.substr(3, 2));
int days = day;
for (int i = 1; i < month; ++i) {
days += daysInMonth[i];
}
return days;
};
int arriveDaysAlice = dateToDays(arriveAlice);
int leaveDaysAlice = dateToDays(leaveAlice);
int arriveDaysBob = dateToDays(arriveBob);
int leaveDaysBob = dateToDays(leaveBob);
int commonStartDate = std::max(arriveDaysAlice, arriveDaysBob);
int commonEndDate = std::min(leaveDaysAlice, leaveDaysBob);
return std::max(0, commonEndDate - commonStartDate + 1);
}
daysInMonth
.dateToDays
that takes a date string and the daysInMonth
array, then converts the date string to the total number of days from the beginning of the year. Calculate this by summing up days from prior months plus the day of the month.arriveAlice
, leaveAlice
, arriveBob
, and leaveBob
by calling dateToDays
.arriveDaysAlice
and arriveDaysBob
.leaveDaysAlice
and leaveDaysBob
.