From e785f9fff0965a668d31ef9bd6efc75e05e825b2 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Mon, 20 Mar 2023 11:19:26 +0800 Subject: [PATCH] add: 230320 --- cpp/2303/230320.cpp | 23 +++++++++++++++++++++++ cpp/2303/CMakeLists.txt | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 cpp/2303/230320.cpp diff --git a/cpp/2303/230320.cpp b/cpp/2303/230320.cpp new file mode 100644 index 0000000..73e4ddd --- /dev/null +++ b/cpp/2303/230320.cpp @@ -0,0 +1,23 @@ +#include + +/** + * 605. Can Place Flowers + * You have a 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-flowers rule. + * + * Refer: 220118.cpp + */ + +class Solution { +public: + static bool canPlaceFlowers(const std::vector& flowerbed, int n) { + int cont0 = 1; + for (int i : flowerbed) { + if (i) { + n -= (cont0 - 1) / 2; + cont0 = 0; + } else ++cont0; + } + return (n - cont0 / 2) <= 0; + } +}; diff --git a/cpp/2303/CMakeLists.txt b/cpp/2303/CMakeLists.txt index 6fcc903..ae545eb 100644 --- a/cpp/2303/CMakeLists.txt +++ b/cpp/2303/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2303) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2303 230319.cpp) +ADD_EXECUTABLE(2303 230320.cpp)