add: 230320

This commit is contained in:
Eatswap 2023-03-20 11:19:26 +08:00
parent f3af871281
commit e785f9fff0
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 24 additions and 1 deletions

23
cpp/2303/230320.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <vector>
/**
* 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<int>& 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;
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2303)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2303 230319.cpp)
ADD_EXECUTABLE(2303 230320.cpp)