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 the time
queryTime
.Example 1:
Input: startTime = [1,2,3], endTime = [3,2,7], queryTime = 4 Output: 1
Approach
Java
package com.practic.array;
public class BusyStudent {
public static void main(String[] args) {
int startTime[] = { 1, 2, 3 };
int endTime[] = { 3, 2, 7 };
int queryTime = 4;
int totalBusy = busyStudent(startTime, endTime, queryTime);
System.out.println(totalBusy);
}
// method to find total busy student
public static int busyStudent(int[] startTime, int[] endTime, int queryTime) {
int total = 0;
// iterate till end of array
for (int i = 0; i < startTime.length; i++) {
// busy check condition here
if (startTime[i] <= queryTime && endTime[i] >= queryTime) {
total++;
}
}
return total;
}
}
C++
#include <bits/stdc++.h>
using namespace std;
int busyStudent(vector<int>& startTime,
vector<int>& endTime, int queryTime)
{
int n=startTime.size(),cnt=0;
for(int i=0;i<n;i++)
{
if(queryTime>=startTime[i]&&queryTime<=endTime[i])
cnt++;
}
return cnt;
}
int main()
{
vector<int> startTime ={1,2,3};
vector<int> endTime = {3,2,7};
int queryTime = 4;
int busy=busyStudent(startTime,endTime,queryTime);
cout<<busy<<"\n";
return 0;
}
No comments:
Post a Comment