add: 230320
This commit is contained in:
parent
f3af871281
commit
e785f9fff0
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2303)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2303 230319.cpp)
|
||||
ADD_EXECUTABLE(2303 230320.cpp)
|
||||
|
|
|
|||
Loading…
Reference in New Issue