C++ solution


  • 0
    C
    class Solution {
    public:
        bool canPlaceFlowers(vector<int>& flowerbed, int n) {
            int cnt=0,tmp_cnt=1;//head is 0 
            for(int i=0;i<flowerbed.size();i++){
                if(flowerbed[i]==0)
                    tmp_cnt++;
                else{
                    cnt+=(tmp_cnt-1)/2;
                    tmp_cnt=0;
                }
            }
            cnt+=tmp_cnt/2;//tail is 0 
            return cnt>=n;
        }
    };
    

Log in to reply
 

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