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

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

相关推荐
以己之23 分钟前
NC313 两个数组的交集
算法·哈希算法
Brookty30 分钟前
【算法】前缀和
java·学习·算法·前缀和·动态规划
And_Ii1 小时前
LeetCode 3397. 执行操作后不同元素的最大数量
数据结构·算法·leetcode
额呃呃1 小时前
leetCode第33题
数据结构·算法·leetcode
隐语SecretFlow1 小时前
【隐语SecretFlow用户案例】亚信科技构建统一隐私计算框架探索实践
科技·算法·安全·隐私计算·隐私求交·开源隐私计算
dragoooon341 小时前
[优选算法专题四.前缀和——NO.27 寻找数组的中心下标]
数据结构·算法·leetcode
少许极端1 小时前
算法奇妙屋(七)-字符串操作
java·开发语言·数据结构·算法·字符串操作
小龙报2 小时前
《算法通关指南---C++编程篇(2)》
c语言·开发语言·数据结构·c++·程序人生·算法·学习方法
金宗汉2 小时前
《宇宙递归拓扑学:基于自指性与拓扑流形的无限逼近模型》
大数据·人工智能·笔记·算法·观察者模式
YY_TJJ4 小时前
算法题——贪心算法
算法·贪心算法