算法打卡 Day34(贪心算法)-分发饼干 + 摆动序列 + 最大子序和

文章目录

理论基础

贪心算法的本质是选择每一阶段的局部最优,从而达到全局最优。

贪心算法没有固定的套路,其是常识性推导 + 举反例。

其一般的解题步骤可以分为四步:

  • 将问题分解为若干个子问题;
  • 找出适合的贪心策略;
  • 求解每一个子问题的最优解;
  • 将局部最优解堆叠成全局最优解

Leetcode 455-分发饼干

题目描述

https://leetcode.cn/problems/assign-cookies/description/

解题思路

这道题的思路可以是尽量用较小的饼干满足胃口较小的孩子的需求,在分发饼干前,需要对胃口值和饼干尺寸进行排序。

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

类似题目

2410-运动员和训练师的最大匹配数

https://leetcode.cn/problems/maximum-matching-of-players-with-trainers/description/

cpp 复制代码
class Solution {
public:
    int matchPlayersAndTrainers(vector<int>& players, vector<int>& trainers) {
        sort(players.begin(),players.end());
        sort(trainers.begin(),trainers.end());
        int count = 0;
        int j = 0;
        for (int i = 0; i < trainers.size();i++){
            if (j < players.size() && trainers[i]>=players[j]){
                j++; count++;
            }
        }
        return count;
    }
};

Leetcode 376-摆动序列

题目描述

https://leetcode.cn/problems/wiggle-subsequence/description/

解题思路

寻找摆动序列可以寻找序列中的转折点,可以通过画图清楚的描述出来。

我们通过 prediff 和 curdiff 记录当前数字前和后的坡度值,从而比较当前值是否为转折点

cpp 复制代码
class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        int curdiff = 0;
        int prediff = 0;
        int count = 1;
        if (nums.size()==1) return count;
        for (int i = 0; i < nums.size()-1;i++){
            curdiff = nums[i+1]- nums[i];
            if (curdiff > 0 && prediff <= 0 || curdiff < 0 && prediff >= 0){
                count++;
                prediff = curdiff;
            }
        }
        return count;
    }
};

Leetcode 53-最大子序和

题目描述

https://leetcode.cn/problems/subsets-ii/description/

解题思路

遍历整个数组,判断是否要继续进行累计还是重新开始的规则是,判断累加和是否为正数,只有当累加和不小于 0 时,继续进行累计才可以保证不拖累后续的数字

cpp 复制代码
class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int result = INT_MIN;
        int count = 0;
        for (int i = 0;i <nums.size();i++){
            count += nums[i];
            if (count > result) result = count;
            if (count < 0) count = 0;
        }
        return result;
    }
};
相关推荐
JiaJZhong几秒前
560. 和为 K 的子数组
数据结构·算法
落羽的落羽2 分钟前
【Linux系统】进程终止、进程等待与进程替换的概念与实现
linux·服务器·c++·人工智能·深度学习·机器学习·游戏引擎
小年糕是糕手2 分钟前
【C++】模板初阶
java·开发语言·javascript·数据结构·c++·算法·leetcode
脏脏a6 分钟前
C++ 字符串处理利器:STL string 保姆级入门教程
开发语言·c++
AndrewHZ1 小时前
【遥感图像入门】遥感图像专用去噪算法:核心方案与实战(PyTorch代码)
pytorch·算法·计算机视觉·cv·遥感图像·高分辨率·去噪算法
qq_479875432 小时前
C++ 网络编程中的 Protobuf 消息分发 (Dispatcher) 设计模式
网络·c++·设计模式
前端小L2 小时前
回溯算法专题(八):精细化切割——还原合法的「IP 地址」
数据结构·算法
Tandy12356_2 小时前
手写TCP/IP协议——IP层输出处理
c语言·网络·c++·tcp/ip·计算机网络
博语小屋2 小时前
实现简单日志
linux·服务器·数据库·c++
Hcoco_me8 小时前
大模型面试题17:PCA算法详解及入门实操
算法