Leetcode 455. Assign Cookies

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

Leetcode 455. Assign Cookies

source url

题目描述

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.

输入:
输出:排序后的队伍
如:

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.

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.

思路

贪心算法:先对输入的两个序列对从小到大排序,随后对饼干进行一次遍历。当当前饼干大小能满足该孩子的需求时,则满足度+1,否则判断下一块饼干的大小。

代码

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        int j = 0;
        int ret = 0;

        sort(g.begin(), g.end());
        sort(s.begin(), s.end());

        for(int i =0;i<s.size();i++){
            if(s[i]>=g[j]&&j<g.size()){
                ret++;
                j++;
            }
        }
        return ret;
    }
};
KEIL5编译报错L6200E:__stdout重定义问题分析与解决 本文深入分析KEIL5编译报错L6200E中__stdout重定义问题的根源,指出因未完整定义__stderr导致与标准库stdio_streams.o冲突。提供完整重定义标准流、启用MicroLIB库等解决方案,帮助开发者彻底解决Symbol multiply defined错误,提升嵌入式开发效率。 阅读详情

相关推荐

有线CarPlay连接全解析:从USB数据线到故障排查实战

车载互联是现代驾驶体验的重要一环,而CarPlay作为手机与车机深度融合的桥梁,其连接稳定性直接关系到导航、通话和娱乐体验。无线方案虽然便利,但在延迟、发热和连接确定性上仍有短板,这使得有线USB连接成为追求可靠性的主流选择。理解USB协议与CarPlay握手原理,是解决连接问题的关键。从USB枚举、认证到iAP2通信,每一步都影响最终体验。本文从线材选型、接口判断出发,深入老车加装方案与故障排查链路,并结合USB抓包等进阶调试手段,帮助车主和开发者从底层理解并解决CarPlay连接难题,让每一次上车都能获

weixin_34183910的博客 451

LeetCode //C - 455. Assign Cookies

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

Made in Code 1661

Halcon实战:5种边缘检测算子对比(Sobel、Roberts、Prewitt、Kirsch、Canny)

本文深入对比了Halcon中Sobel、Roberts、Prewitt、Kirsch和Canny五大边缘检测算子的实战表现与核心差异。通过工业图像实例与代码解析,提供了基于噪声水平、边缘连续性及实时性要求的场景化选型指南,帮助工程师精准选择最适合的算子以优化视觉检测流程。

weixin_29266007的博客 393

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

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

南迁的博客 196

linux系统下XDMA驱动的安装与测试问题总结

先看两个常见的问题。一个是驱动安装问题,一个是驱动测试错误;error 512 能识别PCIE的设备,但是The kernel modle installed correctly, but no devices were recognized.

whm128的博客 3810

PyTorch视觉项目实战:从数据加载到模型部署

PyTorch视觉项目实战:从数据加载到模型部署 ,人工智能,计算机视觉,大模型,AI,随着深度学习在计算机视觉领域的飞速发展,PyTorch 凭借其简洁的代码风格、动态计算图特性以及强大的社区支持,成为众多开发者和研究人员的首选框架。从数据加载、模型构建与训练,到最终的模型部署,PyTorch 提供了一套完整且灵活的解决方案。本文将通过实际项目案例,详细阐述基于 PyTorch 的视觉项目全流程,结合丰富的代码示例和概念解析,帮助读者掌握从项目起步到落地的关键技术。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 4万+

LeetCode 455. Assign Cookies 题解

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

cannonjinx的博客 4234

学 Simulink—— 基于储能系统(BESS)并网逆变器的 削峰填谷(Peak Shaving & Valley Filling)功能仿真

本文详细介绍了基于储能系统(BESS)并网逆变器的削峰填谷功能Simulink仿真方法。主要内容包括:1)削峰填谷原理与典型日负荷曲线控制逻辑;2)简化系统拓扑与关键参数设置;3)分步骤建模过程,重点讲解EMS控制逻辑、SOC模型和PQ控制实现;4)典型仿真结果分析及参数调优建议;5)常见问题解决方案和工程扩展方向。通过该仿真可掌握BESS在用户侧储能、虚拟电厂等场景的核心应用,实现电网侧功率按峰谷时段规划、BESS充放电控制及并网逆变器PQ控制。文章还提供了完整的BESS/微网系列课程框架和混合微网系统进

xiaoheshang_123的博客 43

【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_的博客 243

Spring AI从入门到精通:构建智能Spring应用的全面指南

随着人工智能技术的快速发展,将大语言模型(LLM)与企业应用集成已成为趋势。Spring AI作为Spring官方推出的AI集成框架,为开发者提供了便捷、标准化的方式来构建智能应用。本文将从基础概念到高级应用,全面介绍Spring AI的核心功能与实践技巧。Spring AI 是 VMware 于 2023 年推出的开源框架,旨在简化大语言模型(LLM)与 Spring 应用的集成。它提供了统一的 API 抽象,支持多种 LLM 供应商,包括 OpenAI、Azure OpenAI、Hugging Face

qq_36929638的博客 1070

conda upgrade出现Solving environment:failed CondaValueError Malformed version string invalid chars解决方案

本文主要介绍了conda install出现Solving environment:failed、CondaValueError: Malformed version string invalid character(s),希望能对使用conda的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案 2.1 步骤一 2.2 步骤二

weixin_43178406的博客 8万+

【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的博客 80

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

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

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

weixin_41554427的博客 358

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

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

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

[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

LeetCode笔记:455. Assign Cookies

给一群小孩发饼干

Minecraft 2834

【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的博客 661

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

淡然坊 328

leetcode 455 Assign Cookies

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

YueLing's Blog 685

LeetCode 455. Assign Cookies

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,

reborncgy的博客 201

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 406. Queue Reconstruction by Height
下一篇: Leetcode 435. Non-overlapping Intervals
vincentCheungM
博客等级 码龄10年 4粉丝 23原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值