aboutsummaryrefslogtreecommitdiff
path: root/other/can_place_flower.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/can_place_flower.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/can_place_flower.cpp')
-rw-r--r--other/can_place_flower.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/other/can_place_flower.cpp b/other/can_place_flower.cpp
new file mode 100644
index 0000000..72846ff
--- /dev/null
+++ b/other/can_place_flower.cpp
@@ -0,0 +1,35 @@
+// 605. Can Place Flowers
+#include "leetcode.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
+ */
+
+class Solution {
+public:
+ bool canPlaceFlowers(vector<int> &flowerbed, int n) {
+ flowerbed.insert(flowerbed.begin(), 0);
+ flowerbed.push_back(0);
+ int count = 0, s = flowerbed.size() - 1;
+ for (int i = 1; i < s; i++)
+ if (flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0 && flowerbed[i] == 0) {
+ count++;
+ flowerbed[i] = 1;
+ }
+ if (count >= n)
+ return true;
+ return false;
+ }
+};
+
+int main() {
+ Solution obj;
+ vector<int> flowerbed = {1, 0, 0, 0, 1};
+ printf("%d\n", obj.canPlaceFlowers(flowerbed, 1)); // expect: 1
+ printf("%d\n", obj.canPlaceFlowers(flowerbed, 2)); // expect: 0
+}