当前位置:网站首页>Leetcode notes 605. can place flowers (easy) 605. planting flowers
Leetcode notes 605. can place flowers (easy) 605. planting flowers
2022-07-29 06:24:00 【Lu Yi, twelve】
Just started trying to write this thing , I hope you can point out the shortcomings after reading this article , thank you !!
1. Problem description :( You must be familiar with the topic ! But out of habit, I copied and pasted )
Suppose there's a very long flower bed , Part of the plot is planted with flowers , The other part didn't . But , Flowers cannot be planted on adjacent plots , They compete for water , Both will die .
Give you an array of integers flowerbed Indicates a flower bed , By a number of 0 and 1 form , among 0 No flowers ,1 It means flowers are planted . There's another number n , Can it be planted without breaking the planting rules n A flower ? Energy returns true , If not, return false.
2. Code :( I believe some people have ideas and everything , Just want to see the code , So put the code first )
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
// Two special cases
if(n == 0){
return true;
}
// When n > flowerbed.size()/2 + 1 when , It must be false.
if(n > flowerbed.size() / 2 + 1){
return false;
}
// Add 0, Avoid the trouble of classification discussion .
flowerbed.emplace(flowerbed.begin(), 0);
int count = 0; // The amount that can be planted
int num = 1; // Count whenever num == 3 when ,++count;
// Every three adjacent 0 You can plant a flower , notes : The next time num The count is from the third 0 At the beginning !!!
for(int i = 1; i < flowerbed.size(); ++i){
if(flowerbed[i] != 0){
num = 0;
}else{
++num;
if(num == 3){
++count;
num = 1;
}
}
}
// At the end of the array is 00 You can plant a pot .
if(num == 2){
++count;
}
return {count >= n? true:false};
}
};3. Their thinking :
Since flowers cannot be planted on adjacent plots , Then every three adjacent open spaces can be planted with a pot of flowers , Turn the problem into traversing three adjacent 0, Each group can plant a pot , With num For the neighboring 0 Number of ,count For the number of pots of flowers that can be planted . When flowerbed[i]==0,++num, When num == 3 when ,++count, hold num Reset to 1; When flowerbed[i]== 1,num Zero clearing .
4. Details :
@. When the first value of the array is 1 Or is it 0 It seems that classification discussion is needed to accurately determine count Value , To avoid this trouble , I take to add one at the head of the array 0, In this way, there is no need to classify and discuss .
@. whenever num == 3( There are three consecutive 0) when ,count Add one to the value of , At this time num The value of is not zero , Instead, set it to 1( This is what I think is the most important point ), Because the third 0 Can participate in the following 0 In the count of , For example, the array is 010000010, After planting the most flowers, it becomes 010101010, That is to say 5 A continuous 0 It can be planted 2 basin , Without the need for 6 individual 0.( The expression here seems not very good , If I think of something better, I'll come back and change it .)
@. There are two situations in which the result can be directly judged : The first is n == 0, It must be true; The second is n>(flowerbed.size()/2 + 1), namely n The value has been greater than what the array can theoretically hold 1 The number of , It must be false.
5. Code section
@. adopt emplace() Function or insert() Functions can realize the function of adding elements .emplace() You can only add one element at a time , Faster ;insert() Function can add one or more elements or an array at a certain position , Slower . So I chose emplace() function .
@. In fact, add one at the end of the array 0 There is no need for final judgment count == 2 了 , But the speed is much slower , So I made a compromise .
边栏推荐
- 动态规划总结
- 官方教程 Redshift 05 system参数详细解释
- 八大排序----------------冒泡排序
- Maya ACES工作流程配置(Arnold 及 RedShift 贴图配置规范-还原出SP-Aces流程下贴图正确的效果) PS还原Aces流程下渲染的图
- 【软件工程之美 - 专栏笔记】20 | 如何应对让人头疼的需求变更问题?
- Dynamic planning summary
- 爬取表情包
- Number theory: proof that the maximum number that px+py cannot represent is pq-p-q
- LeetCode #189.轮转数组
- LeetCode #26.删除有序数组中的重复项
猜你喜欢

Ml8 self study notes

LeetCode #1.两数之和

循环链表和双向链表

Computer factory interview questions

Leetcode 1. sum of two numbers

leetcode刷题笔记 452. Minimum Number of Arrows to Burst Balloons (Medium) 452.用最少数量的箭引爆气球(中等)

LeetCode #14. 最长公共前缀

Pit avoidance: about the interconnection of two hc-05 master-slave integrated Bluetooth modules, there is no connection problem

LeetCode #344.反转字符串

FPGA based: multi-target motion detection (hand-in-hand teaching ①)
随机推荐
Leetcode 13. Roman numeral to integer
Abstract encapsulation inheritance polymorphism
Zero basics FPGA (5): counter of sequential logic circuit design (with introduction to breathing lamp experiment and simple combinational logic design)
LeetCode #13. 罗马数字转整数
UE5 纹理系统讲解及常见问题设置及解决方案
传统模型预测控制轨迹跟踪——圆形轨迹(功能包已经更新)
Mathematical modeling experience
MySql-面试题
从头安装MYSQL(MYSQL安装文档-解压版)
Leetcode 14. longest public prefix
#9196 肿瘤面积 题解
2022 spring move - core technology FPGA development post pen test question (original question and experience)
LeetCode #14. 最长公共前缀
进程与进程的概念
STM32 printf问题总结 semihosting microLIB理解
TB6600+stm32F407测试
FPGA based: multi-target motion detection (hand-in-hand teaching ①)
官方教程 Redshift 03 各种GI的参数和常规使用说明
ArduinoIDE + STM32Link烧录调试
Ml9 self study notes