C++ easy O(n) greedy solution, no change to original input

  • 0
        bool canPlaceFlowers(vector<int>& flowerbed, int n) {
            int len=0, ct=0;
            for(int i=0;i<flowerbed.size();i++) {
                if(flowerbed[i]==1) {
                    if(ct==0&&len>1) n-=len/2;
                    else if(ct>0&&len>2) n-=(len-1)/2;
                    if(n==0) return true;
                else len++;
            if(ct==0) return n<=(len+1)/2;
            return n<=len/2;

Log in to reply

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