class Solution :
def merge ( self, intervals: List[ List[ int ] ] ) - > List[ List[ int ] ] :
intervals. sort( key= lambda p: p[ 0 ] )
ans = [ ]
for p in intervals:
if ans and p[ 0 ] <= ans[ - 1 ] [ 1 ] :
ans[ - 1 ] [ 1 ] = max ( ans[ - 1 ] [ 1 ] , p[ 1 ] )
else :
ans. append( p)
return ans
class Solution :
def monotoneIncreasingDigits ( self, N: int ) - > int :
nums = list ( str ( N) )
length = len ( nums)
begin = 0
is_result = True
max_num = float ( '-inf' )
for i in range ( 1 , length) :
num = int ( nums[ i] )
pre_num = int ( nums[ i - 1 ] )
if pre_num > max_num:
begin = i - 1
max_num = pre_num
if pre_num > num:
is_result = False
break
if is_result:
return N
nums[ begin] = str ( int ( nums[ begin] ) - 1 )
for i in range ( begin + 1 , length) :
nums[ i] = '9'
return int ( "" . join( nums) )
class Solution :
def minCameraCover ( self, root: Optional[ TreeNode] ) - > int :
def dfs ( node) :
if node is None :
return inf, 0 , 0
l_choose, l_by_fa, l_by_children = dfs( node. left)
r_choose, r_by_fa, r_by_children = dfs( node. right)
choose = min ( l_choose, l_by_fa) + min ( r_choose, r_by_fa) + 1
by_fa = min ( l_choose, l_by_children) + min ( r_choose, r_by_children)
by_children = min ( l_choose + r_by_children, l_by_children + r_choose, l_choose + r_choose)
return choose, by_fa, by_children
choose, _, by_children = dfs( root)
return min ( choose, by_children)