Skip to content

Commit

Permalink
Add 605 in c language
Browse files Browse the repository at this point in the history
  • Loading branch information
julienChemillier authored Oct 27, 2022
1 parent 98b6666 commit 16e0617
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions c/605-can-place-flowers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not
empty, and an integer n, return if n new flowers can be planted in the flowerbed without
violating the no-adjacent-flowers rule.
Space: O(1)
Time: O(n)
*/

bool canPlaceFlowers(int* flowerbed, int flowerbedSize, int n){
int cpt = 0; // Count the number of flowers that can be added
int i=0;
while (i<flowerbedSize) {
if ((i+1)==flowerbedSize) { // To avoid 'index out of range'
if (flowerbed[i]==0) {
cpt++;
}
i++;
} else if (flowerbed[i]==0) {
if (flowerbed[i+1]==0) {
cpt++;
i += 2;
} else {
i += 3;
}
} else {
i += 2;
}
}
return cpt>=n;
}

0 comments on commit 16e0617

Please sign in to comment.