605. Can Place Flowers
leetcode easy
#array#csharp#easy#leetcode#math#two-pointers
Task
У вас есть длинная клумба, на которой некоторые участки засажены, а некоторые нет. Однако цветы нельзя сажать на соседних участках.
Дан целочисленный массив
flowerbed
, содержащий 0 и 1, где 0 означает пустой участок, а 1 — занятый участок, и целое число
n
. Верните
true
, если
n
новых цветов можно посадить на клумбе, не нарушая правила о соседних цветах, и
false
в противном случае.
Пример:
Input: flowerbed = [1,0,0,0,1], n = 1
Output: true
C# solution
matched/originalpublic class Solution {
public bool CanPlaceFlowers(int[] flowerbed, int n) {
int count = 0;
for (int i = 0; i < flowerbed.Length; i++) {
if (flowerbed[i] == 0) {
bool emptyLeft = i == 0 || flowerbed[i - 1] == 0;
bool emptyRight = i == flowerbed.Length - 1 || flowerbed[i + 1] == 0;
if (emptyLeft && emptyRight) {
flowerbed[i] = 1;
count++;
}
}
}
return count >= n;
}
}C++ solution
auto-draft, review before submit#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
public bool CanPlaceFlowers(vector<int>& flowerbed, int n) {
int count = 0;
for (int i = 0; i < flowerbed.size(); i++) {
if (flowerbed[i] == 0) {
bool emptyLeft = i == 0 || flowerbed[i - 1] == 0;
bool emptyRight = i == flowerbed.size() - 1 || flowerbed[i + 1] == 0;
if (emptyLeft && emptyRight) {
flowerbed[i] = 1;
count++;
}
}
}
return count >= n;
}
}Java solution
matched/originalclass Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int count = 0;
for (int i = 0; i < flowerbed.length; i++) {
if (flowerbed[i] == 0) {
boolean emptyLeft = i == 0 || flowerbed[i - 1] == 0;
boolean emptyRight = i == flowerbed.length - 1 || flowerbed[i + 1] == 0;
if (emptyLeft && emptyRight) {
flowerbed[i] = 1;
count++;
}
}
}
return count >= n;
}
}JavaScript solution
matched/originalvar canPlaceFlowers = function(flowerbed, n) {
let count = 0;
for (let i = 0; i < flowerbed.length; i++) {
if (flowerbed[i] === 0) {
let emptyLeft = i === 0 || flowerbed[i - 1] === 0;
let emptyRight = i === flowerbed.length - 1 || flowerbed[i + 1] === 0;
if (emptyLeft && emptyRight) {
flowerbed[i] = 1;
count++;
}
}
}
return count >= n;
};Python solution
matched/originalclass Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
count = 0
for i in range(len(flowerbed)):
if flowerbed[i] == 0:
empty_left = i == 0 or flowerbed[i - 1] == 0
empty_right = i == len(flowerbed) - 1 or flowerbed[i + 1] == 0
if empty_left and empty_right:
flowerbed[i] = 1
count += 1
return count >= nGo solution
matched/originalfunc canPlaceFlowers(flowerbed []int, n int) bool {
count := 0
for i := 0; i < len(flowerbed); i++ {
if flowerbed[i] == 0 {
emptyLeft := i == 0 || flowerbed[i-1] == 0
emptyRight := i == len(flowerbed)-1 || flowerbed[i+1] == 0
if emptyLeft && emptyRight {
flowerbed[i] = 1
count++
if count >= n {
return true
}
}
}
}
return count >= n
}