Number of Students Doing Homework at a Given Time Leetcode Solution

Difficulty Level Easy
algorithms Array coding Interview interviewprep LeetCode LeetCodeSolutionsViews 3124

The problem Number of Students Doing Homework at a Given Time Leetcode Solution asks us to find the number of students working on their homework at a given time. The problem is very clear, the title itself is already able to explain the whole problem. We are given a query time and the time the students work on their homework. So, using this information we are asked to find the number of students who are doing their homework during the query time. So, as usual before directly jumping into the solution. Let us check a few examples.

Number of Students Doing Homework at a Given Time Leetcode Solution

startTime = [1,2,3], endTime = [3,2,7], queryTime = 4
1

Explanation: We have three students out of which only 1 student works on his/her homework during the query time. Because there is only a single child starting his homework at time = 3, and completing it at time = 7.

startTime = [4], endTime = [4], queryTime = 4
1

Explanation: We have a single student who starts his/her homework at the same as that of the query time. Thus the output is also 1.

Approach for Number of Students Doing Homework at a Given Time Leetcode Solution

The problem asked us to find the number of students who are working on their homework during the query time. We are provided with the start and end times of students’ homework activities in the form of a vector or array. Using this information, we try to find the required value.

We can easily solve the problem just by traversing over the given information. The given information is the starting and end time of the homework activity. So we check if the query time lies between the start and end time of the homework activity. Keeping a count of these students gives us the resultant answer.

Code for Number of Students Doing Homework at a Given Time Leetcode Solution

C++ code

#include <bits/stdc++.h>
using namespace std;

int busyStudent(vector<int> startTime, vector<int> endTime, int queryTime) {
    int n = startTime.size();
    int ans = 0;
    for(int i=0;i<n;i++)
        ans += ((startTime[i] <= queryTime && queryTime <= endTime[i]) ? 1 : 0);
    return ans;
}

int main(){
    vector<int> startTime = {4};
    vector<int> endTime = {4};
    int queryTime = 4;
    cout<<(busyStudent(startTime, endTime, queryTime));
}
1

Java code

import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
  public static int busyStudent(int[] startTime, int[] endTime, int queryTime) {
        int n = startTime.length;
        int ans = 0;
        for(int i=0;i<n;i++)
            ans += ((startTime[i] <= queryTime && queryTime <= endTime[i]) ? 1 : 0);
        return ans;
    }
    
  public static void main (String[] args) throws java.lang.Exception
  {
    int[] startTime = {4};
    int[] endTime = {4};
    int queryTime = 4;
    System.out.println(busyStudent(startTime, endTime, queryTime));
  }
}
1

Complexity Analysis

Time Complexity

O(N), since we traverse all the input elements. Thus the time complexity comes out to be linear.

Space Complexity

O(1), because we use only a single variable to store the count.

Translate »