Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find in which interval how many tasks are worked on in Python
Suppose we have a list of intervals where each interval is like [start, end) and we also have a list of strings called types. Now for a given i, the intervals[i] shows the times someone worked on the job types[i] from [start, end). Two intervals of the same type never overlap or touch. So we have to find a sorted merged list where each item has [start, end, num_types], indicates from start to end, num_types number of tasks were being worked on.
So, if the input is like intervals = [[0, 3], [5, 7], [0, 7]] types = ["problem solving", "news", "game play"], then the output will be [[0, 3, 2], [3, 5, 1], [5, 7, 2]], as we have these few types of work being done: Between [0, 3) "problem solving" and "game play", between [3, 5) "game play", and between [5, 7) "news" and "game play".
Algorithm
To solve this, we will follow these steps −
Create an events list to track start (+1) and end (−1) of each interval
For each interval [start, end], add (start, +1) and (end, −1) to events
Sort the events list by time
Process events chronologically, maintaining a count of active tasks
When count changes, record the interval with the previous count
Example
class Solution:
def solve(self, intervals, jobs):
events = []
# Create events for interval starts and ends
for start, end in intervals:
events.append((start, 1)) # Task starts
events.append((end, -1)) # Task ends
# Sort events by time
events.sort()
active_tasks = 0
last_time = -1
result = []
# Process each event
for time, change in events:
# If time changed and we have active tasks, record the interval
if time != last_time and active_tasks != 0:
result.append([last_time, time, active_tasks])
# Update active task count
active_tasks += change
last_time = time
return result
# Test the solution
solution = Solution()
intervals = [
[0, 3],
[5, 7],
[0, 7]
]
types = ["problem solving", "news", "game play"]
print(solution.solve(intervals, types))
Output
[[0, 3, 2], [3, 5, 1], [5, 7, 2]]
How It Works
The algorithm uses an event-based approach:
Events Creation: Each interval [start, end) creates two events: (start, +1) and (end, −1)
Sorting: Events are sorted by time to process them chronologically
Sweep Line: We sweep through time, maintaining a count of active tasks
Interval Recording: When the count changes, we record the previous interval
Step-by-Step Execution
For intervals [[0,3], [5,7], [0,7]]:
Events: [(0,1), (3,-1), (5,1), (7,-1), (0,1), (7,-1)]
After sorting: [(0,1), (0,1), (3,-1), (5,1), (7,-1), (7,-1)]
At time 0: count becomes 2 (two tasks start)
At time 3: record [0,3,2], count becomes 1
At time 5: record [3,5,1], count becomes 2
At time 7: record [5,7,2], count becomes 0
Conclusion
This event-based sweep line algorithm efficiently merges overlapping intervals and counts concurrent tasks. The time complexity is O(n log n) due to sorting, where n is the number of intervals.
