【代码随想录算法训练营——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;
    }
};
相关推荐
开开心心就好14 小时前
电脑禁止自动休眠工具,还能按负载定时
算法·智能手机·pdf·电脑·word·音视频·lua
geovindu1 天前
python:DecisionTree
开发语言·后端·python·算法
geovindu1 天前
python: Decision Tree Regressor
开发语言·后端·python·算法·决策树
AllData公司负责人1 天前
实战二:AllData大模型与AI数据底座-数据传输平台-DBSyncer,搭建 Http 接口同步至 Doris,API 业务数据自动拉取入数仓
大数据·数据库·数据仓库·人工智能·算法·http·doris
武子康1 天前
火系被选七成,就应该削弱吗?
人工智能·算法·机器学习
夜不会漫长1 天前
C++:C++11特性
开发语言·c++·算法
秋天的怀念_David1 天前
算法一千零一夜 · 第三十七夜 · 类型的过路费
java·程序人生·算法
xckkcxxck1 天前
Kaggle S6E10 冠军方案精读(二):245 个模型的合唱团,和一个只会“线性指挥“的老师
算法
饮水桶_T1 天前
Git笔记
开发语言·笔记·算法