[LeetCode] 605. Can Place Flowers_Easy
2024-10-01 06:27:07
Suppose 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 - they would compete for water and both would die.
Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.
Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1
Output: True
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2
Output: False
Note:
- The input array won't violate no-adjacent-flowers rule.
- The input array size is in the range of [1, 20000].
- n is a non-negative integer which won't exceed the input array size.
思路就是监测, 如果左中右都为0, 那么加1, 并把当前的位置置1, 最后判断ans>=n
Code
class Solution(object):
def canPlaceFlowers(self, flowerbed, n):
"""
:type flowerbed: List[int]
:type n: int
:rtype: bool
"""
fb, ans = [0] + flowerbed + [0], 0
for i in range(1, len(fb) -1):
if fb[i-1] == fb[i] == fb[i+1] == 0:
fb[i] = 1
ans += 1
return ans >= n
最新文章
- 在Ubuntu中搭建.NET开发环境
- JS设置CSS样式的几种方式
- 点击div折叠
- Oracle基础 事务
- android----sqlite中的 query() 参数分析
- DIV隐藏与重显
- HTML5 canvas 中的线条样式
- IEnumerable实践应用
- mvc的filter
- hdu_1029_hash/map
- JS引擎线程的执行过程的三个阶段(二)
- python json模块出现Invalid control character这个异常的原因
- Git操作自动触发企业微信机器人webhook
- rman实验——测试备份压缩
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十六)Structured Streaming中ForeachSink的用法
- OpenNebula学习第三节之虚拟机管理
- Android开发的16条小经验总结
- 使用JPush(极光推送)实现远程通知
- 小白第一次使用Git随笔
- iOS 使用AFNetworking 设置cookie