[LeetCode] 605. Can Place Flowers
풀이
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
if(n==0) return true;
int planted = 0;
int size = flowerbed.size();
if(size == 1){
planted = (flowerbed[0] == 0);
return (planted >= n);
}
for(int i = 0; i< size; ++i){
bool canPlant = false;
if((i==0)&&(size >= 2)){
if((flowerbed[0] == 0)&&(flowerbed[1]== 0)) canPlant = true;
}
else if((i == size-1)&&(size >= 2)){
if((flowerbed[size-2] == 0)&&(flowerbed[size-1]== 0)) canPlant = true;
}
else if(size >= 3){
if((flowerbed[i-1] == 0) && (flowerbed[i] == 0) && (flowerbed[i+1] == 0)) canPlant = true;
}
cout << i << " "<< canPlant<<"\n";
if(canPlant){
flowerbed[i] = 1;
planted++;
if(planted>= n) return true;
}
}
return false;
}
};