LeetCode 贪心 代码随想录笔记

LeetCode 贪心 代码随想录笔记,第1张

455 分发饼干
class Solution {
public:
    int findContentChildren(vector& g, vector& s) {
        //每次选最小的饼干看是否能满足某个孩子
        sort(s.begin(),s.end());
        sort(g.begin(),g.end());
        
        int cnt=0;
        int indexS=0;
        int indexG=0;
        
        for(int i=indexS;i=g[j])
                {
                    ++cnt;
                    ++indexG;
                    ++indexS;
                    break;
                }
            }
        }

        return cnt;
    }
};

 减少循环

class Solution {
public:
    int findContentChildren(vector& g, vector& s) {

        sort(s.begin(),s.end());
        sort(g.begin(),g.end());

        int indexS=0;
        int res=0;

        for(int i=0;i
376 摆动序列
class Solution {
public:
    int wiggleMaxLength(vector& nums) {
        if(nums.size()==1) return nums.size();
       
        int cnt=1;
        int DiffPre=0;
        int DiffCur;

        for(int i=0;i0&&DiffPre<=0)
                ||(DiffCur<0&&DiffPre>=0)
            )
            {
                DiffPre=DiffCur;
                ++cnt;
            }
        }

        return cnt;
    }
};

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/565038.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-06
下一篇 2022-04-06

发表评论

登录后才能评论

评论列表(0条)