aboutsummaryrefslogtreecommitdiff
path: root/can_place_flower.c
diff options
context:
space:
mode:
Diffstat (limited to 'can_place_flower.c')
-rw-r--r--can_place_flower.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/can_place_flower.c b/can_place_flower.c
new file mode 100644
index 0000000..a9e8811
--- /dev/null
+++ b/can_place_flower.c
@@ -0,0 +1,33 @@
+// 605. Can Place Flowers
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+/*
+ * you have long flowerbed in which some of the plots are
+ * planted, and some are not. however, flowers cannot be planted
+ * in adjacent plots. 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 rule
+ */
+
+bool canPlaceFlowers(int *flowerbed, int flowerbed_size, int n) {
+ int i = 0, prev = 0;
+ for (; i < flowerbed_size - 1; i++) {
+ if (flowerbed[i] == 0 && prev == 0 && flowerbed[i + 1] == 0) {
+ prev = 1;
+ n--;
+ } else
+ prev = flowerbed[i];
+ }
+ if (flowerbed[i] == 0 && prev == 0)
+ n--;
+ return n <= 0;
+}
+
+int main() {
+ int flowerbed[] = {1, 0, 0, 0, 1};
+ printf("%d\n", canPlaceFlowers(flowerbed, 5, 1)); // expect: 1
+ printf("%d\n", canPlaceFlowers(flowerbed, 5, 2)); // expect: 0
+}