leetcode 605. Can Place Flowers | 605. 种花问题
题目
https://leetcode-cn.com/problems/can-place-flowers/
题解
class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int count = 0;
int max = 0;
boolean emptyHead = flowerbed[0] != 1;
for (int i : flowerbed) {
if (i == 0) {
count++;
} else {
if (emptyHead) { // head
max += count / 2;
emptyHead = false;
} else { // mid
max += (count - 1) / 2;
}
count = 0;
}
}
// tail
if (flowerbed[flowerbed.length - 1] == 0) {
if (emptyHead) max += (count + 1) / 2; // absolutely empty
else max += count / 2;
}
return max >= n;
}
}
还没有评论,来说两句吧...