C++ solution, easy to understand


  • 0
    D
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {
        int count=0,flag=0;
        flowerbed.push_back(0);
        flowerbed.push_back(1);
        for (int i=0;i<flowerbed.size();i++){
            if (flowerbed[i]==0) flag++;
            else{
                count+=max(flag/2,0);
                flag=-1;
            }
        }
        return n<=count;
    }

Log in to reply
 

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