Given an array of meeting time intervals
where intervals[i] = [starti, endi]
, determine if a person could attend all meetings.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]] Output: false
Example 2:
Input: intervals = [[7,10],[2,4]] Output: true
Constraints:
0 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti < endi <= 106
program main
implicit none
integer, parameter :: n = 3
integer, dimension(n) :: intervals = reshape([0, 30, 5, 10, 15, 20], shape(intervals))
write (*, '(L1)') canAttendMeetings(intervals)
contains
function canAttendMeetings(intervals) result(canAttend)
implicit none
integer, intent(in) :: intervals(:, :)
logical :: canAttend
integer :: i, j, k
! Sort the intervals by their starting times
do i = 1, size(intervals, 1) - 1
do j = i + 1, size(intervals, 1)
if (intervals(j, 1) < intervals(i, 1)) then
intervals(j, :) = intervals(j, :) + intervals(i, :) - intervals(j, :)
intervals(i, :) = intervals(j, :) - intervals(i, :)
end if
end do
end do
! Check for overlapping intervals
canAttend = .true.
do i = 1, size(intervals, 1) - 1
do j = i + 1, size(intervals, 1)
if (intervals(j, 1) >= intervals(i, 2)) exit
if (intervals(j, 2) > intervals(i, 2)) then
canAttend = .false.
exit
end if
end do
end do
end function canAttendMeetings
end program main
temp.f95:21:20: 21 | intervals(j, :) = intervals(j, :) + intervals(i, :) - intervals(j, :) | 1 Error: Dummy argument ‘intervals’ with INTENT(IN) in variable definition context (assignment) at (1) temp.f95:22:20: 22 | intervals(i, :) = intervals(j, :) - intervals(i, :) | 1 Error: Dummy argument ‘intervals’ with INTENT(IN) in variable definition context (assignment) at (1) temp.f95:7:40: 7 | write (*, '(L1)') canAttendMeetings(intervals) | 1 Error: Rank mismatch in argument ‘intervals’ at (1) (rank-2 and rank-1)
module meetings
implicit none
contains
function canAttendMeetings(intervals) result(canAttend)
! Declare variables
integer, allocatable :: intervals(:, :)
logical :: canAttend
! Initialize variables
canAttend = .true.
! Check if the meetings can be attended
do i = 1, size(intervals, 1) - 1
if (intervals(i, 2) > intervals(i + 1, 1)) then
canAttend = .false.
exit
end if
end do
end function canAttendMeetings
end module meetings
program main
! Declare variables
integer, allocatable :: intervals(:, :)
logical :: canAttend
! Read input
read (*, *) intervals
! Check if the meetings can be attended
canAttend = canAttendMeetings(intervals)
! Print output
write (*, *) canAttend
end program main
temp.f95:17:4: 17 | do i = 1, size(intervals, 1) - 1 | 1 Error: Symbol ‘i’ at (1) has no IMPLICIT type temp.f95:38:12: 38 | canAttend = canAttendMeetings(intervals) | 1 Error: Cannot convert REAL(4) to LOGICAL(4) at (1)
def can_attend_meetings(intervals):
intervals.sort()
for i in range(1, len(intervals)):
if intervals[i][0] < intervals[i - 1][1]:
return False
return True
#include <vector>
#include <algorithm>
bool canAttendMeetings(std::vector<std::vector<int>>& intervals) {
std::sort(intervals.begin(), intervals.end());
for (int i = 1; i < intervals.size(); ++i) {
if (intervals[i][0] < intervals[i - 1][1])
return false;
}
return true;
}