Description

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.

Discuss

只需要计算出一共可以放多少个就可以了。

Code

class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int len = flowerbed.length;
if (len < n) { return false; }
int count = 0;
if (len == 1 && flowerbed[0] == 0) {
count++;
return true;
}
for (int i = 0; i < len; i++) {
if (i == 0) {
if (flowerbed[0] == 0 && flowerbed[1] == 0) {
count++;
flowerbed[0] = 1;
}
continue;
}
if (i == len-1) {
if (flowerbed[i - 1] == 0 && flowerbed[i] == 0) {
count++;
}
break;
}
if (flowerbed[i - 1] == 0 && flowerbed[i] == 0 && flowerbed[i + 1] == 0) {
count++;
flowerbed[i] = 1;
}
}
return count >= n;
}
}

【Leetcode】605. Can Place Flowers的更多相关文章

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

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

  2. 【Leetcode_easy】605. Can Place Flowers

    problem 605. Can Place Flowers 题意: solution1: 先通过简单的例子(比如000)发现,通过计算连续0的个数,然后直接算出能放花的个数,就必须要对边界进行处理, ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. Sticky footers 套路

    [CSS Secrets] http://shop.oreilly.com/product/0636920031123.do 以饿了么商家信息的弹出层为例,布局如下: <!-- 饿了么 弹出层部 ...

  2. pl/sql 存储过程执行execute immediate 卡住

    在存储过程中,执行了create table.update table.insert into table 但是在使用pl/sql的存储过程调试的时候,一有问题就直接卡住(标识:执行中.....) 后 ...

  3. 保存Google、Bing翻译的语音

    以Chrome浏览器+google翻译为例,bing的下载步骤也类似 1.打开google翻译页面(translate.google.com),输入一段文本,如下图 2.可以看到,右侧已经翻译好了,这 ...

  4. 模拟误删除InnoDB ibdata数据文件恢复

    注意:假如误删除 ibdata文件 ,此时千万别把mysqld进程杀死,否则没法挽救. 1.模拟删除ibdata数据文件和重做日志文件: [root@hcdb0 data]# lltotal 4219 ...

  5. wireshark抓取本地回环数据包

      linux环境下,用tcpdump,可以用-i lo参数抓取环回接口的包.如果服务端和客户端安装在同一台机器上,调试时是很方便的.linux版的wireshark,选取网卡的菜单里也有lo选项,也 ...

  6. iOS UI(布局)约束是什么?view1.attr1 = view2.attr2 * multiplier + constant

    /* Create constraints explicitly.  Constraints are of the form "view1.attr1 = view2.attr2 * mul ...

  7. 【LOJ6052】「雅礼集训 2017 Day11」DIV(杜教筛)

    点此看题面 大致题意: 求\(1\sim n\)内所有满足\(a>0\)的约数\(a+bi\)的\(a\)之和. 解题思路 首先,我们设\(x=(a+bi)(c+di)(1\le x\le n) ...

  8. Golang Failpoint 的设计与实现

    小结: 1. https://mp.weixin.qq.com/s/veIoupLjM4l5SUVC6h_Gkw Golang Failpoint 的设计与实现 原创: 龙恒 PingCAP 今天  

  9. GreenPlum 与hadoop什么关系?(转)

    没关系. gp 可以处理大量数据, hadoop 可以处理海量. gp 只能处理湖量,或者河量. 无法处理海量. 作者:SallyLeo链接:https://www.zhihu.com/questio ...

  10. (转)超级实用且不花哨的js代码大全

    事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture();  event.srcE ...