当前位置:网站首页>605. Planting flowers
605. Planting flowers
2022-07-06 16:07:00 【mrbone9】
Address :
Power button https://leetcode-cn.com/problems/can-place-flowers/
subject :
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.
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 |
Tips :
1 <= flowerbed.length <= 2 * 104 flowerbed[i] by 0 or 1 flowerbed There are no two adjacent flowers 0 <= n <= flowerbed.length |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/can-place-flowers
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
Consider calculating the current maximum number of new plants , Last sum n Compare , If the maximum number < n , It can't be planted
For one 3 Combination of elements , Possible 1 or 0 New species of , such as :[1,0,0] or [0,1,0] or [0,0,1]
The easiest thing to think of is to look at the current element value :
1. [i] == 1, Possible locations [1,0,0], therefore Subscript +2
2. [i] == 0, Possible locations [0,0,0],[0,0,1], So subscript +2
If [i+1] == 1,[0,1,0] impossible , So subscript +1, +2
3. The last processing of the end element may not be 3 Tuples
[0],[0,0] You need to consider [i-1] Is it 0, If it is 0, that [1],[1,0]
[0,1] Don't have to consider , Because the first two cases 1 The location of has been handled
Method 1 、 Traverse
bool canPlaceFlowers(int* flowerbed, int flowerbedSize, int n){
int maxSize = 0;
if(flowerbedSize == 1)
{
if(flowerbed[0] == 0)
maxSize++;
if(maxSize < n)
return false;
else
return true;
}
for(int i=0; i<flowerbedSize; )
{
// [i] == 1
if(flowerbed[i] == 1)
{
i+=2;
continue;
}
// [i] == 0
if(flowerbed[i] == 0)
{
if(i+1 < flowerbedSize)
{
if(flowerbed[i+1] == 0)
{
maxSize++;
i+=2;
continue;
}
i++;
i+=2; // new start
continue;
}
else if(flowerbed[i-1] != 1)
{
maxSize++;
i++;
}
}
}
if(maxSize < n)
return false;
else
return true;
}
边栏推荐
- C language must memorize code Encyclopedia
- [exercise-7] (UVA 10976) fractions again?! (fraction split)
- Matlab comprehensive exercise: application in signal and system
- [exercise-6] (PTA) divide and conquer
- 【练习-9】Zombie’s Treasure Chest
- [exercise-3] (UVA 442) matrix chain multiplication
- D - Function(HDU - 6546)女生赛
- Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
- b站 實時彈幕和曆史彈幕 Protobuf 格式解析
- 渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
猜你喜欢
洛谷P1102 A-B数对(二分,map,双指针)
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
Programmers, what are your skills in code writing?
7-1 懂的都懂 (20 分)
Borg maze (bfs+ minimum spanning tree) (problem solving report)
b站 实时弹幕和历史弹幕 Protobuf 格式解析
渗透测试 ( 4 ) --- Meterpreter 命令详解
渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
Ball Dropping
Gartner:关于零信任网络访问最佳实践的五个建议
随机推荐
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
栈的经典应用—括号匹配问题
Basic Q & A of introductory C language
Programmers, what are your skills in code writing?
Information security - threat detection engine - common rule engine base performance comparison
Research Report of peripheral venous catheter (pivc) industry - market status analysis and development prospect prediction
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
The most complete programming language online API document
树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
X-Forwarded-For详解、如何获取到客户端IP
Opencv learning log 13 corrosion, expansion, opening and closing operations
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
【练习-7】Crossword Answers
[exercise-3] (UVA 442) matrix chain multiplication
If you want to apply for a programmer, your resume should be written like this [essence summary]
滲透測試 ( 1 ) --- 必備 工具、導航
Flink 使用之 CEP
C language is the watershed between low-level and high-level
Borg Maze (BFS+最小生成树)(解题报告)