
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Intervals Intersecting at a Given Point in Python
Suppose we have a list of intervals and a value called point. Each interval interval[i] contains [si, ei] represents start time and end time of interval i (both inclusive). We have to find the number of intervals that are intersecting at given point.
So, if the input is like intervals = [[2, 6],[4, 10],[5, 9],[11, 14]] point = 5, then the output will be 3, because at time 5, there are 3 intervals those are [3, 6], [4, 10], [5, 9]
To solve this, we will follow these steps −
count := 0
-
for each start time i and end time j in intervals, do
-
if point >= i and point <= j, then
count := count + 1
-
return count
Example
Let us see the following implementation to get better understanding
def solve(intervals, point): count = 0 for i, j in intervals: if point >= i and point <= j: count += 1 return count intervals = [[2, 6],[4, 10],[5, 9],[11, 14]] point = 5 print(solve(intervals, point))
Input
[[2, 6],[4, 10],[5, 9],[11, 14]], 5
Output
3
Advertisements