Number of Students Doing Homework at a Given Time

🏠 ⬅️ ➡️

Given two integer arrays startTime and endTime and given an integer queryTime.

The ith student started doing their homework at the time startTime[i] and finished it at time endTime[i].

Return the number of students doing their homework at time queryTime. More formally, return the number of students where queryTime lays in the interval [startTime[i], endTime[i]] inclusive.

Example 1:

Input: startTime = [1,2,3], endTime = [3,2,7], queryTime = 4 Output: 1 Explanation: We have 3 students where: The first student started doing homework at time 1 and finished at time 3 and wasn't doing anything at time 4. The second student started doing homework at time 2 and finished at time 2 and also wasn't doing anything at time 4. The third student started doing homework at time 3 and finished at time 7 and was the only student doing homework at time 4.

Example 2:

Input: startTime = [4], endTime = [4], queryTime = 4 Output: 1 Explanation: The only student was doing their homework at the queryTime.

Constraints:

  • startTime.length == endTime.length
  • 1 <= startTime.length <= 100
  • 1 <= startTime[i] <= endTime[i] <= 1000
  • 1 <= queryTime <= 1000

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 3
    integer, dimension(n) :: startTime = [1, 2, 3]
    integer, dimension(n) :: endTime = [3, 2, 7]
    integer :: queryTime = 4
    integer :: i

    do i = 1, n
        if (queryTime >= startTime(i) .and. queryTime <= endTime(i)) then
            print '(I0)', i
        end if
    end do

end program main
Compiled
Executed
Correct
module homework
    implicit none

contains

    function countStudents(startTime, endTime, queryTime) result(count)
        integer, intent(in) :: startTime(:), endTime(:), queryTime
        integer :: count

        count = countStudentsRecursive(startTime, endTime, queryTime, 1, size(startTime))
    end function countStudents

    recursive function countStudentsRecursive(startTime, endTime, queryTime, startIndex, endIndex) result(count)
        integer, intent(in) :: startTime(:), endTime(:), queryTime, startIndex, endIndex
        integer :: count

        if (startIndex > endIndex) then
            count = 0
        elseif (queryTime < startTime(startIndex)) then
            count = countStudentsRecursive(startTime, endTime, queryTime, startIndex + 1, endIndex)
        elseif (queryTime > endTime(endIndex)) then
            count = countStudentsRecursive(startTime, endTime, queryTime, startIndex, endIndex - 1)
        else
            count = countStudentsRecursive(startTime, endTime, queryTime, startIndex + 1, endIndex - 1) + 1
        endif
    end function countStudentsRecursive
end module homework

program test
    use homework
    implicit none

    integer, parameter :: startTime(3) = [1, 2, 3]
    integer, parameter :: endTime(3) = [3, 2, 7]
    integer, parameter :: queryTime = 4
    integer :: count

    count = countStudents(startTime, endTime, queryTime)
    write (*, '(A, I0)') 'The number of students doing their homework at time ', queryTime, ' is ', count

    count = countStudents(startTime, endTime, 4)
    write (*, '(A, I0)') 'The number of students doing their homework at time ', 4, ' is ', count

    count = countStudents(startTime, endTime, 10)
    write (*, '(A, I0)') 'The number of students doing their homework at time ', 10, ' is ', count
end program test
🌐 Data from online sources
def removeLeafNodes(root, target):
    if not root:
        return None
    root.left = removeLeafNodes(root.left, target)
    root.right = removeLeafNodes(root.right, target)
    if not root.left and not root.right and root.val == target:
        return None
    return root
We traverse the binary tree using a depth-first search (DFS) approach. We use a recursion to visit left and right children of each node.
  1. If the root node is null, return null.
  2. Call removeLeafNodes recursively and update left and right children.
  3. After updating children, check if the current node is a leaf node (i.e., both children are null) and its value equals the target. If so, delete it (return null); otherwise, return the current node.
  4. Repeat this process until deleted leaf nodes can no longer create new leaf nodes with the target value.
🌐 Data from online sources
TreeNode* removeLeafNodes(TreeNode* root, int target) {
    if (!root) return nullptr;
    root->left = removeLeafNodes(root->left, target);
    root->right = removeLeafNodes(root->right, target);
    if (!root->left && !root->right && root->val == target) {
        return nullptr;
    }
    return root;
}
We traverse the binary tree using a depth-first search (DFS) approach. We use a recursion to visit left and right children of each node.
  1. If the root node is null, return null.
  2. Call removeLeafNodes recursively and update left and right children.
  3. After updating children, check if the current node is a leaf node (i.e., both children are null) and its value equals the target. If so, delete it (return null); otherwise, return the current node.
  4. Repeat this process until deleted leaf nodes can no longer create new leaf nodes with the target value.