首页 > 其他分享 >LeetCode 605. 种花问题

LeetCode 605. 种花问题

时间:2022-11-09 17:55:29浏览次数:38  
标签:605 pre int flowerbed 种花 LeetCode

贪心

class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {
        int m = flowerbed.size();
        int pre = -1;
        for(int i=0;i<m;i++){
            //寻找区间
            if(flowerbed[i]==1){
                if(pre<0){
                    n-=i/2;
                }else{
                    n-=(i-pre-2)/2;
                }
                pre = i;
            }
        }
        if(pre<0){
            n-=(m+1)/2;
        }else{
            n-=(m-pre-1)/2;
        }
        return n<=0;

    }
};

标签:605,pre,int,flowerbed,种花,LeetCode
From: https://www.cnblogs.com/poteitoutou/p/16874654.html

相关文章

  • 2022/11 LeetCode练习
    ......
  • 洛谷P1605题解分析
    迷宫题目描述给定一个\(N\timesM\)方格的迷宫,迷宫里有\(T\)处障碍,障碍处不可通过。在迷宫中移动有上下左右四种方式,每次只能移动一个方格。数据保证起点上没有障......
  • 双指针_Leetcode刷题_11/100
    算法解释双指针主要用于遍历数组,两个指针指向不同的元素,从而协同完成任务。也可以延伸到多个数组的多个指针。若两个指针指向同一个数组,遍历的方向相同且不会相交,则也称......
  • 贪心算法_Leetcode刷题_7/100
    贪心算法采用贪心策略,保证每次操作是局部最优的,从而使随后结果是全局最优的。455.分配饼干贪心策略:尽量把最小的饼干分配给胃口最小的孩子。我的代码:算法描述:将......
  • leetcode35
    搜索插入位置Category Difficulty Likes Dislikesalgorithms Easy(45.79%) 1329 -TagsCompanies给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果......
  • LeetCode刷题记录.Day9
    快乐数题目链接202.快乐数-力扣(LeetCode)classSolution{public:intgetSum(intn){intsum=0;while(n){sum+=(n%10)*......
  • leetcode-520-easy
    DetectCapitalWedefinetheusageofcapitalsinawordtoberightwhenoneofthefollowingcasesholds:Alllettersinthiswordarecapitals,like"USA".......
  • leetcode-1784-easy
    CheckifBinaryStringHasatMostOneSegmentofOnesGivenabinarystringswithoutleadingzeros,returntrueifscontainsatmostonecontiguoussegment......
  • leetcode-819-easy
    MostCommonWordGivenastringparagraphandastringarrayofthebannedwordsbanned,returnthemostfrequentwordthatisnotbanned.Itisguaranteedthe......
  • LeetCode 435. Non-overlapping Intervals
    贪心按照有边界排序,只有先选择了右边边界小的,才可以放下更多的区间classSolution{public:interaseOverlapIntervals(vector<vector<int>>&intervals){......