【LeetCode】455. Assign Cookies

小白水平理解面试经典题目LeetCode 455 Assign Cookies 假设你是一位很棒的父母,想给你的孩子一些饼干。但是,你最多应该给每个孩子一块饼干。每个孩子 i 都有一个贪婪因子 g[i] ,这是孩子满意的 cookie 的最小大小;每个 cookie j 都有一个大小 s[j]。如果 s[j] >= g[i] ,我们可以将 cookie j 分配给孩子子 i。你的目标是最大化内容子项的数量并输出最大数量。 阅读详情

455. Assign Cookies

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

Note:
You may assume the greed factor is always positive. 
You cannot assign more than one cookie to one child.

Example 1:

Input: [1,2,3], [1,1]

Output: 1

Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. 
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
You need to output 1.

Example 2:

Input: [1,2], [1,2,3]

Output: 2

Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. 
You have 3 cookies and their sizes are big enough to gratify all of the children, 
You need to output 2.

思路:贪心选择

#include<iostream>
#include<algorithm>
#include<vector> 
using namespace std;
int findContentChildren(vector<int>& g, vector<int>& s) {
    sort(g.begin(),g.end());
    sort(s.begin(),s.end());
    int ans=0;
	for(int gi=0,si=0;gi<g.size()&&si<=s.size();){
		if(g[gi]<=s[si]) {
			ans++;
			gi++;	//如果可以满足,则应该开始满足下一个要吃蛋糕的小朋友了 
		}
		si++;//如果可以满足,换到下一个蛋糕;
			//如果不可以满足,说明当前这个蛋糕谁都满足不了(因为这是排序过了的),只能放弃 
	}
	return ans;
}
 int main(void)
 {
 	vector<int> g,s;
 	int g_size,s_size;
 	while(cin>>g_size>>s_size){
 		int num;
 		for(int i=0;i<g_size;i++){
 			cin>>num;
 			g.push_back(num);
		 }
		for(int i=0;i<s_size;i++){
 			cin>>num;
 			s.push_back(num);
		 }
		 cout<<findContentChildren(g,s)<<endl;
	 }
	 return 0;
 }

 

灵境视界:基于云计算与深度学习的智能手语识别AR眼镜 灵境视界”不仅仅是一款AR眼镜,它代表了未来无障碍沟通设备的发展方向。随着云计算与深度学习技术的不断进步,未来我们将进一步优化识别算法,提升翻译精度,并扩展更多语言和手语类型,为全球听障人士提供更加全面的沟通解决方案。是一款融合了云计算与深度学习技术的增强现实(AR)眼镜,专为听障人士和手语使用者设计。这款眼镜不仅具备先进的AR显示功能,还能实时识别并翻译手语,打破听障人士与健听人士之间的沟通障碍,推动无障碍社会的建设。,让沟通无界,视界无限。 阅读详情

相关推荐

QT使用openssl实现RSA加解密

使用openssl生成公私钥,随后用QT对相应的信息进行RSA加解密

weixin_42098322的博客 3871

LeetCode //C - 455. Assign Cookies

【代码】LeetCode //C - 455. Assign Cookies。

Made in Code 1664

【YOLO 系列】基于YOLOv8的工业零件表面缺陷智能检测系统【python源码+Pyqt5界面+数据集+训练代码】

本文详细介绍了如何基于YOLOv8深度学习模型和PyQt5图形界面,构建一套完整的工业零件表面缺陷智能检测系统。内容涵盖从环境搭建、数据集标注、模型训练调参到最终封装成可执行桌面软件的全流程,并深入解析了YOLOv8在工业视觉场景中的技术优势与落地优化策略。

ol78901234的博客 412

LeetCode 455. Assign Cookies 题解

本题是贪心算法的经典应用问题,主要考察对贪心算法思想的理解和使用。通过使用贪心算法,我们可以高效地计算最多能满足的孩子数量。贪心算法的核心思想是:将孩子的胃口值和饼干的尺寸都排序,然后使用两个指针,分别指向孩子的胃口值数组和饼干的尺寸数组的起始位置,比较当前饼干的尺寸是否大于等于当前孩子的胃口值。这种方法不仅适用于分发饼干问题,还可以应用于许多其他需要贪心策略的问题。掌握贪心算法的思想,对于解决这类问题非常重要。

cannonjinx的博客 4607

告别手动标注!用ENVI Classic高效搞定高光谱图像语义分割标签(以农业/地物分类为例)

本文详细介绍了如何利用ENVI Classic实现高光谱图像的半自动化智能标注,特别针对农业和地物分类场景。通过光谱特征分析、智能标注技巧和MAT文件转换,显著提升标注效率和质量,助力AI模型训练。

weixin_28694169的博客 254

ADS仿真功率放大器实例学习笔记

ADS仿真功率放大器实例学习笔记

qq_36521926的博客 1万+

【力扣算法LeetCode】455 Assign Cookies分发饼干

文章目录前言一、题目二、解答1.代码2.复盘总结 前言 题目出自力扣 算法练习题: 一、题目 题目 题目链接 测试用例 二、解答 1.代码 代码如下(示例): 2.复盘 总结

南迁的博客 196

FPGA实现CAN接口(SJA1000)

学无止境,善于积累,每天积累一点点,成功就在眼前,加油! 1 CAN总线简介 CAN(Controller Area Network,控制器局域网 )总线是一个多主机异步串行总线,也是国际上应用最广泛的现场总线之一。在现场总线中,它是惟一被ISO国际标准化组织批准的现场总线。由于其成本低、容错能力强、支持分布式控制、通信速率高等优点在汽车、工业控制、航天等领域得到广泛应用。特别是由于CAN总线具有...

m0_46498597的博客 1万+

【LeetCode】 455 Assign Cookies

题目描述 Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i hasa greed factor g[i], which is the minimum size of a cookie that the child will be content with; and ea.

Vergegenkunft_的博客 244

YOLOv11 模型改进全流程:从代码修改到性能跃升,让你打造专属检测利器

无论是科研中需要发表创新点满满的论文,还是企业里要解决特定场景的检测难题,这套方法都能让你在YOLOv11的基础上打造出“量身定制”的检测模型。别再让通用模型的“平庸性能”限制你的项目上限——现在就动手修改代码,让你的YOLOv11模型在精度、速度或小样本适应能力上实现跨越式提升,用技术突破证明你的AI研发实力!本文将带你从代码层面入手,完成从骨干网络到损失函数的全维度改进,真正掌握让YOLOv11“为你所用”的技术实力。该改进对小样本类别(如数据集中仅占5%的“稀有缺陷”)的识别精度提升显著。

m0_71589036的博客 407

2026年数字IC设计紫光展锐笔试带答案解析

本文是一份数字IC设计工程师/芯片验证工程师的笔试样题,包含单选题和多选题两部分。单选题共20题(每题2分),涵盖数字电路、C++/Verilog编程、芯片设计基础等内容,例如D/A转换计算、面向对象编程特性、三极管工作区域、Verilog仿真时间格式等。多选题15题(每题2分),考察Hold违例修复方法、验证方法学等知识点。试卷总分100分,考试时长90分钟,题型设计兼顾基础理论与工程实践,重点测试应聘者对芯片设计核心概念和工具链的掌握程度。部分题目解析详细说明了计算过程或概念原理,如第1题通过二进制转换

Open 115

[leetcode]455. Assign Cookies

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a coo...

Brett 357

【报错】(pycharm运行程序)OSError: [WinError 1114] 动态链接库(DLL)初始化例程失败。 Error loading

1 报错 OSError: [WinError 1114] 动态链接库(DLL)初始化例程失败。 Error loading 2 分析原因 通过 activate Distance 进行自己创建的 Distance 虚拟环境中,输入python会显示环境中python的版本信息,再依次import,显然不会报错。 这是因为在Anaconda的环境下有支持 import torch 的 dll,而在PyCharm中使用的是自己创建的新环境,还缺少相应支持的dll,仅需在PyCharm中配置环境变量。.

静谧、淡雅 8634

Leetcode 455 Assign Cookies

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a c

夜色之浓,莫过于黎明前的黑暗。 257

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Leetcode 455. Assign Cookies

【代码】Leetcode 455. Assign Cookies。

SUNNY的博客 1413

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

leetcode 455. Assign Cookies和2410. Maximum Matching of Players With Trainers

这道题和上面的是一模一样的。

weixin_41554427的博客 359

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

LeetCode笔记:455. Assign Cookies

给一群小孩发饼干

Minecraft 2834

【leetcode】455. Assign Cookies

problem 455. Assign Cookies solution1: But, you should give each child at most one cookie. 对小朋友的满意程度(也就是胃口)和当前cookies的大小分别进行排序,满足一个小朋友则加1;否则比较下一个cookie是否满足小朋友。记住前提是每个小朋友最多只能得到一个cookie.也就是贪婪算法...

weixin_30241919的博客 81

【leetcode】455. Assign Cookies【E】

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a c

sscssz的博客 662

LeetCode 455 Assign Cookies

题目 Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is t...

乌鲁木齐001号程序员 91

leetcode 455. Assign Cookies

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a c

淡然坊 329

【小白爬Leetcode455】3.1 分发饼干 Assign Cookies

【小白爬Leetcode455】3.1 分发饼干 Assign Cookies题目Description中文描述思路:贪心算法 Leetcode 455 esay\color{#228B22}{esay}esay 点击进入原题链接:Leetcode455 分发饼干 Assign Cookies 题目 Description Assume you are an awesome parent and want to give your children some cookies. But, you sho

weixin_41604040的博客 282

Leetcode 455. Assign Cookies[easy]

题目: Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a

Proverbs 742

leetcode 455 Assign Cookies

Problem: 给了两个数组g, s。求最多有多少个s中的元素分别>=g中的元素。

YueLing's Blog 685
上一篇: 【LeetCode】 45. Jump Game II
下一篇: 【LeetCode】392. Is Subsequence
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值