LeetCode-605:Can Place Flowers (可放置花的数量)
来源:互联网 发布:郑大网络远程教育中心 编辑:程序博客网 时间:2024/05/18 00:33
Question
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 = 1Output: True
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2Output: 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数组,0表示空,1表示非空,给定整数n,判断是否可以插入n个1,1不能相邻。
Answer
Solution 1:
简单分情况考虑。
- 题目看似情况不多,在实际编写过程中还是发现容易忽略很多情况。
- 首先考虑插入的规则:num个连0,则表示可以插入
(num-1)/2
个1; - 再考虑首尾元素是零的情况,当首位是0或者末位是0,那么只要有两个0则便可以插入1个
1
,也就是说,首位和末位的一个0可以当做两个0来使用; - 最后考虑还有一种情况是,数组为一个0的情况,这个时候可以插入1个
1
; - 连续0结束则计算一次剩余需要插入的1的个数。
- 解法直观,但是没有新意,也容易考虑不周出错。
class Solution { public boolean canPlaceFlowers(int[] flowerbed, int n) { int count = 0; if (flowerbed == null || flowerbed.length == 0) return false; if (flowerbed.length == 1 && flowerbed[0] == 0) return true; for (int i = 0; i < flowerbed.length && n > 0; i++){ if (flowerbed[i] == 0){ if (i == 0 || i == flowerbed.length-1) count++; count++; }else{ n -= (count-1)/2; count = 0; } } n -= (count-1)/2; return n < 1; }}
- 时间复杂度:O(n),空间复杂度:O(1)
Solution 2:
使用贪心算法。
- 使用贪心的思想去解决,即只要位置满足条件就立即插入1,直到
n == 0
或者数组遍历完毕。 - 同时需要考虑首位和末位是零的情况。
- 判断条件即判断左右两边分别均是0,只要插入了位置该位置则置1.
class Solution { public boolean canPlaceFlowers(int[] flowerbed, int n) { for (int idx = 0; idx < flowerbed.length && n > 0; idx ++) if (flowerbed[idx]==0 && (idx==0 || flowerbed[idx-1]==0) && (idx==flowerbed.length-1 || flowerbed[idx+1]==0)){ n--; flowerbed [idx] = 1; } return n == 0; }}
- 时间复杂度:O(n),空间复杂度:O(1)
阅读全文
0 0
- LeetCode-605:Can Place Flowers (可放置花的数量)
- LeetCode 605 : Can Place Flowers
- Leetcode 605 Can Place Flowers
- leetcode[Can Place Flowers]
- leetcode-Can Place Flowers
- leetCode-Can Place Flowers
- LeetCode Can Place Flowers
- leetcode 605[easy]---Can Place Flowers
- 605Can Place Flowers
- [leetcode]605. Can Place Flowers
- [LeetCode]605. Can Place Flowers
- [LeetCode]605. Can Place Flowers
- LeetCode 605.Can Place Flowers
- [leetcode]: 605. Can Place Flowers
- [LeetCode] 605. Can Place Flowers
- LeetCode: 605. Can Place Flowers
- leetcode 605. Can Place Flowers
- LeetCode 605. Can Place Flowers
- 为什么换工作?--
- Java 并发 线程安全 ThreadLocal
- [LeetCode]26. Remove Duplicates from Sorted Array
- QGis二次开发基础 -- 添加在线地图服务
- 完全图解scrollLeft,scrollWidth,clientWidth,offsetWidth
- LeetCode-605:Can Place Flowers (可放置花的数量)
- mybatis详解-(9)参数传递
- Linux命令记录(持续更新中)
- 约束优化方法之拉格朗日乘子发与KKT条件
- dubbo泛化调用 http接口 随意调用dubbo服务
- python爬虫<将scrapy的请求设置为同步>
- JAVA基础题
- Android Studio3.0 Android Profiler分析器(cpu memory network 分析器)
- Java Druid大数据连接池的实例应用 和工具类