Number of Programmer Worked on given Time in Python


Suppose we have list of intervals and another input time. In each interval the structure is [start, end], this is representing the times when a programmer worked. We have to find the number of programmers that were working at time.

So, if the input is like interval = [[2, 6],[4, 10],[5, 9],[11, 14]], time = 5, then the output will be 3 as at time 5, there are three programmers, working [2, 6], [4, 10], [5, 9]

To solve this, we will follow these steps −

  • count := 0
  • for each interval in intervals, do
    • if start of interval <= time and end of interval >= time, then
      • count := count + 1
  • return count

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, intervals, time):
      count = 0
      for interval in intervals:
         if interval[0] <= time and interval[1] >= time:
            count += 1
      return count
ob = Solution()
interval = [[2, 6],[4, 10],[5, 9],[11, 14]]
time = 5
print(ob.solve(interval, time))

Input

[[2, 6],[4, 10],[5, 9],[11, 14]], 5

Output

3

Updated on: 23-Sep-2020

31 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements