human
Check if given intervals can be made non | Function to check if two intervals overlap with each other.
Condition to check if the intervals overlap.
Function to check if there is a existing overlapping intervals.
Path compression.
Union of two intervals Returns True if there is a overlapping with the same another interval.
Both have same another overlapping interval.
Function to check if the intervals can be added by X to form non - overlapping intervals.
If the intervals overlaps we will union them.
There is no cycle.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to check if given intervals can be made non and the other steps required.
 def checkOverlapping ( a , b ) :
	a , b = max ( a , b ) , min ( a , b )
	if b [ 0 ] <= a [ 0 ] <= b [ 1 ] :
		return True
	return False
def find ( a , i ) :
	if a [ i ] == i :
		return i
	a [ i ] = find ( a , a [ i ] )
	return a [ i ]
def union ( a , x , y ) :
	xs = find ( a , x )
	ys = find ( a , y )
	if xs == ys :
		return True
	a [ ys ] = xs
	return False
def checkNonOverlapping ( arr , n ) :
	dsu = [ i for i in range ( n + 1 ) ]
	for i in range ( n ) :
		for j in range ( i + 1 , n ) :
			if checkOverlapping ( arr [ i ] ,   \ arr [ j ] ) :
				if union ( dsu , i , j ) :
					return False
	return True
if __name__ == " _ _ main _ _ " :
	arr = [ [ 1 , 4 ] , [ 2 , 2 ] , [ 2 , 3 ] ]
	n = len ( arr )
	print ( " YES " if checkNonOverlapping ( arr , n ) else " NO " )