Python solution, using intervals


  • 0
    Y
        def kEmptySlots(self, A, k):
            """
            :type flowers: List[int]
            :type k: int
            :rtype: int
            """
            def setinterval(intervals,last,slot,k):
                n=len(intervals)
                for x,y in intervals:
                    if x<slot<y:
                        sub1=(x,slot)
                        if sub1[1]-sub1[0]-1==k and sub1[0]!=0:return True
                        sub2=(slot,y)
                        if sub2[1]-sub2[0]-1==k and sub2[1]!=last:return True
                        intervals.remove((x,y))
                        if sub1[1]-sub1[0]-1>k: intervals.add(sub1)
                        if sub2[1]-sub2[0]-1>k: intervals.add(sub2)
                        break
                return False
                        
            n=len(A)
            intervals=set()
            intervals.add((0,n+1))
            for i in xrange(n):
                slot=A[i]
                if setinterval(intervals,n+1,slot,k):
                    return i+1
            return -1
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.