8 lines C++ O(n)


  • 0
    Z
        bool canPlaceFlowers(vector<int>& flowerbed, int n) {
            int count=0;
            flowerbed.insert(flowerbed.begin(),0);
            flowerbed.push_back(0);
            for(int i=1;i<flowerbed.size()-1;i++){
                if(flowerbed[i-1]==0 && flowerbed[i]==0 && flowerbed[i+1]==0) count++,i++;
                if(count>=n) return true;
            }
            return false;
        }
    

Log in to reply
 

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