C# solution


  • 0
    L
        public bool CanPlaceFlowers(int[] flowerbed, int n) {
            int liPrev = -1;
            int liResult = 0;
    
            for( int i = 0; i <= flowerbed.Length; ++i ) {
                if ( i == flowerbed.Length || flowerbed[i] == 1 ) {
                    int liDis = i - liPrev + ( i == flowerbed.Length ? 1 : 0);
                    
                    liResult += ( liDis - 1 ) >> 1;
                    
                    liPrev = i + 1;
                }
            }
            
            return n <= liResult;
        }
    

Log in to reply
 

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