
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find one minimum possible interval to insert into an interval list in Python
Suppose we have a 2D list of numbers called intervals where each row represents [start, end] (inclusive) interval. For an interval [a, b] (a < b), its size is (b - a). We must add one interval to the given list such that, after merging all the intervals, we get exactly one range left. We have to find the minimum possible size of the added interval.
So, if the input is like intervals = [[15, 20],[30, 50]], then the output will be 10, as we can add the interval [20, 30] which is the smallest possible interval.
To solve this, we will follow these steps −
- events := a new list
- for each start and end time s, e in intervals, do
- insert (s, 1) at the end of events
- insert (e, -1) at the end of events
- sort the list events
- curr_status := 0, last := null
- interval := a pair [0, 0]
- for each pair (time, status) in events, do
- if curr_status is same as 0 and last and time > last, then
- if interval[0] is same as 0, then
- interval[0] := last
- interval[1] := time
- if interval[0] is same as 0, then
- last := time
- curr_status := curr_status + status
- if curr_status is same as 0 and last and time > last, then
- return interval[1] - interval[0]
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, intervals): events = [] for s, e in intervals: events.append((s, 1)) events.append((e, -1)) events.sort() curr_status = 0 last = None interval = [0, 0] for time, status in events: if curr_status == 0 and last and time > last: if interval[0] == 0: interval[0] = last interval[1] = time last = time curr_status += status return interval[1] - interval[0] ob = Solution() intervals = [[15, 20],[30, 50]] print(ob.solve(intervals))
Input
[[15, 20],[30, 50]]
Output
10
- Related Articles
- Program to find minimum interval to include each query in Python
- Python Program to Print Numbers in an Interval
- Program to find minimum cost to reduce a list into one integer in Python
- Python program to insert an element into sorted list
- Insert Interval in C++
- Program to find length of longest interval from a list of intervals in Python
- Python program to print all Prime numbers in an Interval
- Program to find intervals by merging target interval in Python
- How to Find Armstrong Number in an Interval using Python?
- Program to count odd numbers in an interval range using Python
- Program to find intervals that do not intersect the cut interval in Python
- Interval List Intersections in C++
- Program to Find Out the Best Interval to Remove in C++
- Python Pandas - Check if an element belongs to an Interval
- Program to find Prime Numbers Between given Interval in C++

Advertisements