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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
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
Compiled
Executed
Correct
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
🌐 Data from online sources
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
  1. Sort the meeting intervals by their start times.
  2. Iterate through the sorted intervals.
  3. Check if the start time of the current interval is less than the end time of the previous interval. If so, return false, as the person cannot attend both of these meetings.
  4. If all intervals pass the check, return true, as the person can attend all meetings.
🌐 Data from online sources
#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;
}
  1. Sort the meeting intervals by their start times.
  2. Iterate through the sorted intervals.
  3. Check if the start time of the current interval is less than the end time of the previous interval. If so, return false, as the person cannot attend both of these meetings.
  4. If all intervals pass the check, return true, as the person can attend all meetings.