C#LeetCode刷题之#605-种花问题( Can Place Flowers)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3724 访问。
假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。
给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花?能则返回True,不能则返回False。
输入: flowerbed = [1,0,0,0,1], n = 1
输出: True
输入: flowerbed = [1,0,0,0,1], n = 2
输出: False
注意:
数组内已种好的花不会违反种植规则。
输入的数组长度范围为 [1, 20000]。
n 是非负整数,且不会超过输入数组的大小。
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.
Input: flowerbed = [1,0,0,0,1], n = 1
Output: True
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.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3724 访问。
public class Program {
public static void Main(string[] args) {
int[] nums = null;
nums = new int[] { 1, 0, 0, 0, 1 };
var res = CanPlaceFlowers(nums, 1);
Console.WriteLine(res);
Console.ReadKey();
}
private static bool CanPlaceFlowers(int[] flowerbed, int n) {
//该题比较简单,处理好边界即可
//需要种植的花为0,总是可以
if(n == 0) return true;
//数组为0,不可种植
if(flowerbed.Length == 0) {
return false;
}
//当数组为1时,根据是否种植直接判定即可
if(flowerbed.Length == 1) {
if(flowerbed[0] == 0) {
return true;
} else {
return false;
}
}
//记录可以种植的数量
int count = 0;
//前2个分析
if(flowerbed.Length >= 2 &&
flowerbed[0] == 0 &&
flowerbed[1] == 0) {
flowerbed[0] = 1;
count++;
}
//连续3个为0时,可种植
for(int i = 1; i < flowerbed.Length - 1; i++) {
if(flowerbed[i - 1] == 0 &&
flowerbed[i] == 0 &&
flowerbed[i + 1] == 0) {
count++;
flowerbed[i] = 1;
}
}
//后2个分析
if(flowerbed.Length >= 2 &&
flowerbed[flowerbed.Length - 1] == 0 &&
flowerbed[flowerbed.Length - 2] == 0) {
flowerbed[flowerbed.Length - 1] = 1;
count++;
}
//可种植数大于等于即将种植的数量时,返回true
if(count >= n) return true;
//无解时,返回false
return false;
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3724 访问。
True
分析:
该题比较简单,若使用其它ADT,可以考虑左右边界补0的方法,这样可以少处理部分边界问题。
显而易见,以上算法的时间复杂度为: 。
C#LeetCode刷题之#605-种花问题( Can Place Flowers)的更多相关文章
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- leetcode 刷题进展
最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多 前200的吃透了 足以应付非算法岗 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- LeetCode刷题总结之双指针法
Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链 ...
- Leetcode刷题记录(python3)
Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...
- LeetCode刷题总结-数组篇(上)
数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- LeetCode刷题总结-数组篇(下)
本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...
随机推荐
- 怎样从gitHub上面拉项目
1.注册 https://gitee.com/oschina 2.拿到代码在gitHub上的地址 3.打开eclipse-->import https://git.oschina.net/cai ...
- 用maven打包java项目的pom文件配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- oracle 在物理机上添加磁盘操作
物理机上添加磁盘操作 注意:1)物理机上添加磁盘操作,不涉及到start_udev的动作.2)磁盘分区的操作,需要谨慎进行,核准无误后再操作. (1)查看磁盘名称命名 # su - grid$ sql ...
- C++语法小记---标准库
C++标准库 C++标准库包含如下内容: C++标准编译工具链 C++扩展编译工具链(各种C++编译器独有) C++标准库 C++库 C库 C兼容库(为了兼容能够用C编译器编译的项目,直接使用C++也 ...
- 如何理解Flutter中的asyc 和 await
https://blog.csdn.net/xdhc304/article/details/90232723 Flutter的语法非常精简, 对于异步任务, 只要使用asyc和awai 配合就能实现, ...
- C语言学习笔记二---数据类型运算符与表达式
一.C的基本语法单位 1.标识符:有效长度:31(DOS环境下) 2.关键字:main不是 3.分隔符:空格符,制表符,换行符,换页符 4.注释符:a./*.....*/ b.// 二.C的常用输 ...
- control+B进入layout文件的xml文本编辑模式
control+B进入layout文件的xml文本编辑模式
- Lun4R-CyBRICSCTF wp
WEB Hunt (Web, Baby, 50 pts) 打断点,然后就一个一个被抓住了... 接着F12就出现了.(这个flag是白色的,藏在下面....)... RE Baby Rev 题目给了个 ...
- 【laravel】Eloquent 模型事件和监听方式
所有支持的模型事件 在 Eloquent 模型类上进行查询.插入.更新.删除操作时,会触发相应的模型事件,不管你有没有监听它们.这些事件包括: retrieved 获取到模型实例后触发 creatin ...
- 关于C语言内存占用
struct T { char a; int *d; int b; int c:16; double e; }; T *p; 在64位系统以及64位编译器下,以下描述正确的是 A. sizeof(p) ...