新闻详情

新闻详情

首页 / 资讯中心 / 详情

贪心题目:种花问题

发布时间:2026/9/5 15:05:28来源:尧图网络
贪心题目:种花问题
文章目录题目标题和出处难度题目描述要求示例数据范围解法思路和算法代码复杂度分析题目标题和出处标题种花问题出处605. 种花问题难度3 级题目描述要求有一个很长的花坛一部分地块种植了花另一部分地块没有种植花。可是花不能种植在相邻的地块上。给定一个整数数组flowerbed \texttt{flowerbed}flowerbed表示花坛由0 \texttt{0}0和1 \texttt{1}1组成其中0 \texttt{0}0表示没种植花1 \texttt{1}1表示种植了花。另外给定一个整数n \texttt{n}n判断是否能在不违反不相邻种花的规则下新种植n \texttt{n}n朵花。示例示例 1输入flowerbed [1,0,0,0,1], n 1 \texttt{flowerbed [1,0,0,0,1], n 1}flowerbed [1,0,0,0,1], n 1输出true \texttt{true}true示例 2输入flowerbed [1,0,0,0,1], n 2 \texttt{flowerbed [1,0,0,0,1], n 2}flowerbed [1,0,0,0,1], n 2输出false \texttt{false}false数据范围1 ≤ flowerbed.length ≤ 2 × 10 4 \texttt{1} \le \texttt{flowerbed.length} \le \texttt{2} \times \texttt{10}^\texttt{4}1≤flowerbed.length≤2×104flowerbed[i] \texttt{flowerbed[i]}flowerbed[i]为0 \texttt{0}0或1 \texttt{1}1flowerbed \texttt{flowerbed}flowerbed中不存在相邻的两朵花0 ≤ n ≤ flowerbed.length \texttt{0} \le \texttt{n} \le \texttt{flowerbed.length}0≤n≤flowerbed.length解法思路和算法为了判断是否可以在确保没有相邻的花的情况下新种植n nn朵花需要计算在确保没有相邻的花的情况下最多可以新种植的花朵数。如果最多可以新种植的花朵数大于等于n nn则返回true \text{true}true否则返回false \text{false}false。用m mm表示数组flowerbed \textit{flowerbed}flowerbed的长度。假设花坛中的位置x xx和y yy种植了花其中0 ≤ x y m 0 \le x y m0≤xym且位置x xx和y yy之间没有种植花即flowerbed [ x ] flowerbed [ y ] 1 \textit{flowerbed}[x] \textit{flowerbed}[y] 1flowerbed[x]flowerbed[y]1且对于任意x z y x z yxzy都有flowerbed [ z ] 0 \textit{flowerbed}[z] 0flowerbed[z]0。当y − x 4 y - x 4y−x4时位置x xx和y yy之间不能新种植花当y − x ≥ 4 y - x \ge 4y−x≥4时为了使新种植的花朵数最多应使用贪心思想应从位置x 2 x 2x2开始向右种植花且新种植的花之间的距离应取最小值2 22此时位置x xx和y yy之间可以新种植花的位置范围是[ x 2 , y − 2 ] [x 2, y - 2][x2,y−2]因此新种植的花朵数是⌊ y − x − 2 2 ⌋ \Big\lfloor \dfrac{y - x - 2}{2} \Big\rfloor⌊2y−x−2​⌋。如果新种植的花与最近的花之间的距离大于2 22则种植相同数量的花需要的位置范围一定大于等于[ x 2 , y − 2 ] [x 2, y - 2][x2,y−2]在位置范围[ x 2 , y − 2 ] [x 2, y - 2][x2,y−2]中可以种植的花朵数一定小于等于⌊ y − x − 2 2 ⌋ \Big\lfloor \dfrac{y - x - 2}{2} \Big\rfloor⌊2y−x−2​⌋因此贪心策略下新种植的花朵数最多。当x 0 x 0x0或y ≥ m y \ge my≥m时由于花坛的边界没有花因此需要使用其他方法计算最多可以新种植的花朵数。分别考虑以下三种情况。当x 0 x 0x0且0 ≤ y m 0 \le y m0≤ym时位置范围[ 0 , y − 2 ] [0, y - 2][0,y−2]中都可以新种植花最多可以新种植的花朵数是⌊ y 2 ⌋ \Big\lfloor \dfrac{y}{2} \Big\rfloor⌊2y​⌋。当0 ≤ x m 0 \le x m0≤xm且y ≥ m y \ge my≥m时位置范围[ x 2 , m − 1 ] [x 2, m - 1][x2,m−1]中都可以新种植花最多可以新种植的花朵数是⌊ m − x − 1 2 ⌋ \Big\lfloor \dfrac{m - x - 1}{2} \Big\rfloor⌊2m−x−1​⌋。当x 0 x 0x0且y ≥ m y \ge my≥m时位置范围[ 0 , m − 1 ] [0, m - 1][0,m−1]中都可以新种植花最多可以新种植的花朵数是⌊ m 1 2 ⌋ \Big\lfloor \dfrac{m 1}{2} \Big\rfloor⌊2m1​⌋。实现方面遍历数组flowerbed \textit{flowerbed}flowerbed并计算最多可以新种植的花朵数遍历过程中维护最多可以新种植的花朵总数count \textit{count}count以及上一朵花的位置prev \textit{prev}prev。为了方便计算将prev \textit{prev}prev初始化为− 2 -2−2确保可以新种植花的位置为非负整数。当遍历到下标i ii时如果flowerbed [ i ] 1 \textit{flowerbed}[i] 1flowerbed[i]1则位置i ii种植了花执行如下操作。上一朵花和当前位置的花之间最多可以新种植的花朵数是⌊ i − prev − 2 2 ⌋ \Big\lfloor \dfrac{i - \textit{prev} - 2}{2} \Big\rfloor⌊2i−prev−2​⌋将其加到count \textit{count}count。将prev \textit{prev}prev的值更新为i ii。遍历结束之后最后一朵花到花坛末尾之间最多可以新种植的花朵数是⌊ m − prev − 2 2 ⌋ \Big\lfloor \dfrac{m - \textit{prev} - 2}{2} \Big\rfloor⌊2m−prev−2​⌋将其加到count \textit{count}count。当count ≥ n \textit{count} \ge ncount≥n时返回true \text{true}true否则返回false \text{false}false。当prev \textit{prev}prev初始化为− 2 -2−2时可以确保计算得到正确的花朵数不需要判断prev \textit{prev}prev的值。实现方面有一处可以优化。由于题目只要求判断是否可以新种植n nn朵花不要求计算最多可以新种植的花朵数因此当count ≥ n \textit{count} \ge ncount≥n时可以直接返回true \text{true}true不需要继续遍历。代码classSolution{publicbooleancanPlaceFlowers(int[]flowerbed,intn){intcount0;intmflowerbed.length;intprev-2;for(inti0;im;i){if(flowerbed[i]1){count(i-prev-2)/2;if(countn){returntrue;}previ;}}count(m-prev-1)/2;returncountn;}}复杂度分析时间复杂度O ( m ) O(m)O(m)其中m mm是数组flowerbed \textit{flowerbed}flowerbed的长度。最多需要遍历数组flowerbed \textit{flowerbed}flowerbed一次。空间复杂度O ( 1 ) O(1)O(1)。
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

Astro 示例模板库实战指南:从 examples 目录到 create-astro 的底层实现 2026/9/5 15:35:32

Astro 示例模板库实战指南:从 examples 目录到 create-astro 的底层实现

Astro 示例模板库实战指南:从 examples 目录到 create-astro 的底层实现 【免费下载链接】astro The web framework for content-driven websites. ⭐️ Star to support our work! 项目地址: https://gitcode.com/GitHub_Trending/as/astro 本文以 Astro 官…

阅读更多 →
IOPaint 快速上手:一键安装 AI 图像修复工具,3分钟装好跑通第一张图 2026/9/5 15:35:32

IOPaint 快速上手:一键安装 AI 图像修复工具,3分钟装好跑通第一张图

IOPaint 快速上手:一键安装 AI 图像修复工具,3分钟装好跑通第一张图 【免费下载链接】IOPaint Image inpainting tool powered by SOTA AI Model. Remove any unwanted object, defect, people from your pictures or erase and replace(powered by stab…

阅读更多 →
射频工程师从零到一:ADS、Cadence与PCB设计实战路径 2026/9/5 15:35:32

射频工程师从零到一:ADS、Cadence与PCB设计实战路径

射频工程师这个岗位,听起来门槛很高,既要懂理论又要会仿真,还得能画板子。很多想入行或者刚入行的朋友,面对ADS、Cadence、PCB设计这一大堆工具和概念,往往不知道从哪里下手,更别提独立完成一个射频前端模块…

阅读更多 →
Hugo 有序分类法(Ordered Taxonomy)元素方法详解:Count、Page、Pages、Term 与 WeightedPages 2026/9/5 15:35:32

Hugo 有序分类法(Ordered Taxonomy)元素方法详解:Count、Page、Pages、Term 与 WeightedPages

Hugo 有序分类法(Ordered Taxonomy)元素方法详解:Count、Page、Pages、Term 与 WeightedPages 【免费下载链接】hugo The world’s fastest framework for building websites. 项目地址: https://gitcode.com/gh_mirrors/hu/hugo Hugo…

阅读更多 →
快速上手 superfile:终端文件管理器 5 分钟实战 2026/9/5 15:35:32

快速上手 superfile:终端文件管理器 5 分钟实战

快速上手 superfile:终端文件管理器 5 分钟实战 【免费下载链接】superfile Pretty fancy and modern terminal file manager 项目地址: https://gitcode.com/GitHub_Trending/su/superfile 整理下载目录是件烦心事:安装包、压缩包、临时文件堆在…

阅读更多 →
ICM20948九轴传感器实战:从SPI驱动到姿态解算全流程解析 2026/9/5 15:32:31

ICM20948九轴传感器实战:从SPI驱动到姿态解算全流程解析

简介:本资源是一套面向嵌入式开发者与物联网应用工程师的ICM20948九轴传感器驱动开发实践包,聚焦陀螺仪、加速度计与磁力计三合一运动感知模块的底层驱动实现与跨接口适配问题。压缩包共9个文件(26KB),含6个C源文件&am…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞