Python O(N) solution


  • 0

    traverse the flowerbed. if a proper slot is found, r++. if the non-adjacent rule is violated, r--. finally, compare n with r.

    		r = 0
    		c = 1
    		for f in flowerbed:
    			if f==1:
    				if c==2:
    					r-=1
    				else:
    					c = 0
    			else:
    				if c==2:
    					c=0
    				c+=1
    				if c==2:
    					r += 1
    		return n<=r
    

Log in to reply
 

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