贪心之分配问题

/**
 * @brief 分配问题
 * 
 * @param hungerDegree 
 * @param cookies 
 * 有一群孩子和一堆饼干,每个孩子有一个饥饿度,每个饼干都有一个大小。每个孩子只能吃一个饼干,
 * 且只有饼干的大小不小于孩子的饥饿度时,这个孩子才能吃饱。求解最多有多少孩子 可以吃饱。
 */
void Algorithm::assignCookies(std::vector<int>& hungerDegree, std::vector<int>& cookies)
{
    //先排序
    std::sort(hungerDegree.begin(), hungerDegree.end());

    std::sort(cookies.begin(), cookies.end());

    int child = 0, cookie = 0, remain = 0, full = 0;

    while ( child < hungerDegree.size() && cookie < cookies.size() )
    {
        if ( hungerDegree[child] <= cookies[cookie])
        {
            ++child;
        }
        else
        {
            ++cookie;
        }
    }

    std::cout << "max child number: " << child << std::endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值