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:

  1. The input array won't violate no-adjacent-flowers rule.
  2. The input array size is in the range of [1, 20000].
  3. 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

[LeetCode] 605. Can Place Flowers_Easy的更多相关文章

  1. LeetCode 605. 种花问题(Can Place Flowers) 6

    605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...

  2. LeetCode 605. Can Place Flowers (可以种花)

    Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...

  3. Java实现 LeetCode 605 种花问题(边界问题)

    605. 种花问题 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给定一个花坛(表示为一个数组包含0和1,其中0表示没种 ...

  4. Leetcode 605.种花问题

    种花问题 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表 ...

  5. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  6. leetcode算法总结

    算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 ...

  7. LeetCode(605,581,566)

    LeetCode(605,581,566) 摘要:605盲改通过:581开始思路错误,后利用IDE修改(多重循环跳出方法):566用C语言时需要动态内存分配,并且入口参数未能完全理解,转用C++. 6 ...

  8. 【LEETCODE】54、数组分类,简单级别,题目:605、532

    数组类,简单级别完结.... 不容易啊,基本都是靠百度答案.... 希望做过之后后面可以自己复习,自己学会这个解法 package y2019.Algorithm.array; /** * @Proj ...

  9. 【LeetCode】605. Can Place Flowers 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 贪婪算法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. R语言(入门小练习篇)

    问题: 一组学生参加了数学.科学和英语考试.为了给所有的学生确定一个单一的成绩衡量指标,需要将这些科目的成绩组合起来.另外,还想将前20%的学生评定为A,接下来20%的学生评定为B,以此类推.最后,希 ...

  2. 如何修改 Ubuntu 的字符集?

    步骤: 1.编辑 local 文件 输入: vi /var/lib/locales/supported.d/local 将 zh_CN.GB2312 加入到后面,保存. 2.执行:locale-gen ...

  3. m个苹果放入n个篮子

    题目 :X个相同的苹果放入Y个篮子,(1)篮子可以为空 ,篮子不同. 放法有C(X+Y-1,Y-1 );// (2)篮子不可以为空,篮子不同.放法有C(X-1,Y-1) //插挡板法 分析有了这个组合 ...

  4. Excel中countif函数的使用方法

    1.countif函数的含义 在指定区域中按指定条件对单元格进行计数(单条件计数) 建议和countifs函数结合起来学习,可以观看小编的经验Excel中countifs函数的使用方法. END 2. ...

  5. laravel读取memcached缓存并做条件查询

    public function onlineplayersource() { $res = $_POST['aoData']; $sEcho = 0; $iDisplayStart = 0; // 起 ...

  6. sencha touch Demo(示例)(2014-6-25)

    这是一个开源示例,是我对sencha touch的深层应用.已停止更新 sencha touch版本:2.2.1/2.3.1 源码地址: https://bitbucket.org/moLangZai ...

  7. zabbix高级玩法之通过xml文件一次性导入

    author:headsen  chen date: 2018-11-10  10:08:38 背景:zabbix的硬件监控架构已经部署完成,接下来就是创建主机这一步,2400台主机如果采用一台台的创 ...

  8. 虚拟机VMware怎么完全卸载干净,如何彻底卸载VMware虚拟机

    亲测好使. 1.禁用VM虚拟机服务 首先,需要停止虚拟机VMware相关服务.按下快捷键WIN+R,打开windows运行对话框,输入[services.msc],点击确定.如下图. 在服务管理中,找 ...

  9. 【巷子】---json-server---基本使用

    一.前后端并行开发的痛点 前端需要等待后端开发完接口以后 再根据接口来完成前端的业务逻辑 二.解决方法 在本地模拟后端接口用来测试前端效果 这种做法称之为构建前端Mock   三.json-serve ...

  10. poj3259 Wormholes【最短路-bellman-负环】

    While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole ...