【代码随想录算法训练营——Day27(Day26休息)】贪心算法——455.分发饼干、376.摆动序列、53.最大子数组和

LeetCode题目链接

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

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

https://leetcode.cn/problems/maximum-subarray/

题解
455.分发饼干

受了AI的提示,感觉就是模拟题,但是指针要回溯。题解是分开遍历饼干和胃口。

376.摆动序列

看了题解不是很懂,再看chatGPT的写法,用一个up和一个down记录波动。


https://www.bilibili.com/video/BV17M411b7NS/?vd_source=3a6627dd97ba1bc690e84872a319e5dc

看视频,视频讲的很详细。

这道题一共学会了两种解法。

啊啊啊我自己写的代码通过了,当然受AI的提示result的初始值为1,结果再+1,再根据b站视频下录友留言要去重,就加了个去重逻辑。

这一题的三种思路都值得深思。

53.最大子数组和

拿到题目第一反应是双指针法,但是这样时间复杂度肯定很高。看了题解,说这种解法勉强可以过。先用暴力写法写一遍试试。暴力解法现在不能过了,写贪心。

还有不懂的看题解,题解写的很详细。

代码

cpp 复制代码
//455.分发饼干
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());
        int num = 0;
        for (int i = 0, j = 0;i < g.size() && j < s.size();i++, j++) {
			if (s[j] >= g[i]) num++;
			else i--;  //重点
        }
        return num;
    }
};

int main() {
	vector<int> g1 = { 1,2,3 }, s1 = { 1,1 }, g2 = { 1,2 }, s2 = { 1,2,3 };
    Solution s;
	cout << s.findContentChildren(g1, s1) << endl;
    return 0;
}
cpp 复制代码
//376.摆动序列
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
		vector<int> tmp;
		tmp.push_back(nums[0]);
		for (int i = 1;i < nums.size();i++) {
			if (nums[i] != nums[i - 1]) tmp.push_back(nums[i]);
		}

        vector<int> diff;
        if (tmp.size() == 1) return 1;
		if (tmp.size() == 2) return 2;
		for (int i = 1; i < tmp.size(); i++) {
			diff.push_back(tmp[i] - tmp[i - 1]);
		}
		int result = 1;
		for (int i = 0; i < diff.size(); i++) {
			if (i > 0 && diff[i] * diff[i - 1] < 0) result++;
		}
		return result + 1;
    }
};

int main() {
	vector<int> nums1 = { 1,7,4,9,2,5 }, nums2 = { 1,17,5,10,13,15,10,5,16,8 }, nums3 = { 1,2,3,4,5,6,7,8,9 }, nums4 = { 3,3,3,2,5 }, nums5 = { 0, 0 }, nums6 = {0, 0, 0};
    Solution s;
    printf("%d", s.wiggleMaxLength(nums1));
    return 0;
}
cpp 复制代码
//53.最大子数组和(暴力解法,不能过)
class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int result = INT_MIN;
        for (int i = 0;i < nums.size();i++) {
            int count = 0;
            for (int j = i;j < nums.size();j++) {
				count += nums[j];
                if (count > result) result = count;
            }
        }
        return result;
    }
};
相关推荐
颜酱11 小时前
图的数据结构:从「多叉树」到存储与遍历
javascript·后端·算法
zone773916 小时前
006:RAG 入门-面试官问你,RAG 为什么要切块?
后端·算法·面试
CoovallyAIHub19 小时前
OpenClaw 近 2000 个 Skills,为什么没有一个好用的视觉检测工具?
深度学习·算法·计算机视觉
CoovallyAIHub19 小时前
CVPR 2026 | 用一句话告诉 AI 分割什么——MedCLIPSeg 让医学图像分割不再需要海量标注
深度学习·算法·计算机视觉
CoovallyAIHub19 小时前
Claude Code 突然变成了 66 个专家?这个 5.8k Star 的开源项目,让我重新理解了什么叫"会用 AI"
深度学习·算法·计算机视觉
兆子龙19 小时前
前端哨兵模式(Sentinel Pattern):优雅实现无限滚动加载
前端·javascript·算法
xlp666hub1 天前
Leetcode第五题:用C++解决盛最多水的容器问题
linux·c++·leetcode
CoovallyAIHub1 天前
9个视觉语言模型工厂实测:Qwen 87.9%碾压全场,你的显卡能跑哪个?
算法
SparkX开源AI知识库1 天前
手摸手带你安装OpenClaw并对接飞书
算法·架构
一语07161 天前
3分钟搞懂深度学习AI:实操篇:卷积层
人工智能·算法