23贪心算法

分发饼干

cpp 复制代码
class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        int i=0,j=0;
        int count=0;
        sort(s.begin(),s.end());
        sort(g.begin(),g.end());
        while(i<g.size()&&j<s.size()){
            if(g[i]<=s[j]){
                i++;
                j++;
                count++;
            }else{
                j++;
            }
        }
        return count;
    }
};

摆动序列

cpp 复制代码
class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        int count=1;
        int prev=nums[0];
        bool neg=0;
        int start=1;
        for(int i=1;i<nums.size();i++){
            if(i==start){
                if(prev==nums[i]){
                    start++;
                    continue;
                }else{
                    neg=nums[i]>prev?1:0;
                    prev=nums[i];
                    count++;
                    continue;
                }
                
            }
            if(neg){
                if(nums[i]<prev){
                    neg=0;
                    prev=nums[i];
                    count++;
                }else{
                    prev=nums[i];
                }
            }else{
                if(nums[i]>prev){
                    neg=1;
                    prev=nums[i];
                    count++;
                }else{
                    prev=nums[i];
                }
            }
        }
        return count;
    }
};

最大子序列和

cpp 复制代码
class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int res=-__INT32_MAX__;
        int max=res;
        for(int num:nums){
            if(res<0)res=num;
            else{
                res+=num;
            }
            if(res>max)max=res;
        }
        return max;
    }
};
相关推荐
墨染点香14 小时前
LeetCode 刷题【126. 单词接龙 II】
算法·leetcode·职场和发展
aloha_78915 小时前
力扣hot100做题整理91-100
数据结构·算法·leetcode
Tiny番茄15 小时前
31.下一个排列
数据结构·python·算法·leetcode
挂科是不可能出现的15 小时前
最长连续序列
数据结构·c++·算法
_Aaron___15 小时前
List.subList() 返回值为什么不能强转成 ArrayList
数据结构·windows·list
前端小L16 小时前
动态规划的“数学之魂”:从DP推演到质因数分解——巧解「只有两个键的键盘」
算法·动态规划
RTC老炮16 小时前
webrtc弱网-ReceiveSideCongestionController类源码分析及算法原理
网络·算法·webrtc
21号 116 小时前
9.Redis 集群(重在理解)
数据库·redis·算法
码农多耕地呗17 小时前
力扣146.LRU缓存(哈希表缓存.映射+双向链表数据结构手搓.维护使用状况顺序)(java)
数据结构·leetcode·缓存
晚枫~17 小时前
数据结构基石:从线性表到树形世界的探索
数据结构