代码随想录笔记--贪心算法篇

1--贪心算法

主要思路:

通过局部最优推导全局最优;

2--分发饼干

主要思路:

基于贪心算法,每次都尽可能用大的饼干去喂胃口大的孩子,贪心地节省饼干;

cpp 复制代码
#include <iostream>
#include <vector>
#include <algorithm>

class Solution {
public:
    int findContentChildren(std::vector<int>& g, std::vector<int>& s) {
        if(s.size() == 0) return 0;
        // 从小到大排序饼干
        std::sort(g.begin(), g.end());
        std::sort(s.begin(), s.end());
        
        int res = 0; // 结果
        int cookie_idx = s.size() - 1; // 从最后一个饼干开始分发

        // 贪心地每次用大饼干去喂胃口大的孩子
        for(int child_idx = g.size() - 1; child_idx >= 0; child_idx--){
            if(cookie_idx >= 0 && s[cookie_idx] >= g[child_idx]){ // 饼干能满足孩子的胃口
                res++;
                cookie_idx--;
            }
        }
        return res;
    }
};

int main(int argc, char* argv[]){
    // g = [1,2,3], s = [1,1]
    std::vector<int> child = {1, 2, 3};
    std::vector<int> cookie = {1, 1};
    Solution S1;
    int res = S1.findContentChildren(child, cookie);
    std::cout << res << std::endl;
    return 0;
}

3--摆动序列

主要思路:
基于贪心算法,贪心地计算山谷和山峰的个数;

cpp 复制代码
#include <iostream>
#include <vector>

class Solution {
public:
    int wiggleMaxLength(std::vector<int>& nums) {
        int pre_diff = 0;
        int cur_diff = 0;
        int result = 1; // 默认最右边元素是一个摆动
        for(int i = 0; i < nums.size() - 1; i++){
            cur_diff = nums[i+1] - nums[i];
            if(pre_diff <= 0 && cur_diff > 0 || pre_diff >= 0 && cur_diff < 0){
                result ++;
                pre_diff = cur_diff; // 产生摆动才更新 pre_diff 避免平波递增的情况
            }
        }
        return result;
    }
};

int main(int argc, char* argv[]){
    // nums = [1,7,4,9,2,5]
    std::vector<int> test = {1, 7, 4, 9, 2, 5};
    Solution S1;
    int res = S1.wiggleMaxLength(test);
    std::cout << res << std::endl;
    return 0;
}

4--最大子序和

主要思路:
贪心地纳入当前数,如果和为负值,就将和置为 0,否则保留;

cpp 复制代码
#include <iostream>
#include <vector>
#include <limits.h>

class Solution {
public:
    int maxSubArray(std::vector<int>& nums) {
        int sum = 0;
        int max_sum = INT_MIN;
        for(int i = 0; i < nums.size(); i++){
            sum = sum + nums[i];
            if(sum > max_sum) max_sum = sum;
            if(sum < 0) sum = 0;
        }
        return max_sum;
    }   
};

int main(int argc, char* argv[]){
    // nums = [-2,1,-3,4,-1,2,1,-5,4]
    std::vector<int> nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
    Solution S1;
    int res = S1.maxSubArray(nums);
    std::cout << res << std::endl;
    return 0;
}

5--买卖股票最佳时机II

相关推荐
朝朝辞暮i1 天前
C++ 第 21 课:struct —— 把一组相关数据打包在一起
开发语言·c++·算法
青梅橘子皮1 天前
Blue---二分算法(二分查找与二分答案)
算法
长河1 天前
Java的GC(垃圾回收)中,标记无用对象的核心方法是可达性分析算法
java·jvm·算法
月光船幽幽1 天前
加性偏移外推提升参数识别可靠性
python·算法
Ivanqhz1 天前
层归一化、残差、前馈网络与激活函数简述
服务器·数据库·人工智能·深度学习·算法
不会就选b1 天前
算法日常・每日刷题--<动态规划>3
数据结构·算法
-dzk-1 天前
【堆】LC 347.前 K 个高频元素
算法·堆
H.莓飛1 天前
【数据结构】栈_OJ题
linux·数据结构·算法·centos
Zootopia6261 天前
多架 eVTOL集群排班调度方案思考
人工智能·算法·数学建模·matlab·动态规划·无人机·evtol
和裕1 天前
年度框架直供 vs 零散按需采购:定制纸箱采购成本、交付与服务核心区别全对比
大数据·运维·网络·人工智能·算法