C++ clean solution


  • 0
    Z
    class Solution {
    public:
        bool canPlaceFlowers(vector<int>& flowerbed, int n) {
            int front = 0, rear = flowerbed.size() - 1, plot_cnt = 0, sum = 0;
            while(front < rear) {
                if(1 == flowerbed[front]) {
                    front += 2;
                    continue;
                }
                for(plot_cnt = 0; front < rear && flowerbed[front] == 0; ++plot_cnt) {
                    ++front;
                }
                sum += plot_cnt >> 1;
                if(1 == flowerbed[rear]) {
                    rear -= 2;
                    continue;
                }
                for(plot_cnt = 0; front < rear && flowerbed[rear] == 0; ++plot_cnt) {
                    --rear;
                }
                sum += plot_cnt >> 1;
            }
            return (front == rear && 0 == flowerbed[front] ? sum + 1 : sum) >= n;
        }
    };
    
    

Log in to reply
 

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