Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Simple greedy thought .. satify each kid with minimum qualified cookie, from the least greedy kid..

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        int ng = g.size();
        int ns = s.size();
        
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());
        
        int ig = 0, is = 0;
        while(ig < ng && is < ns)
        {
            while(is < ns && s[is] < g[ig]) is ++;
            if(is == ns) break;
            
            ig ++; is ++;
        }
        
        return ig;
    }
};
posted on 2017-01-24 12:52  Tonix  阅读(172)  评论(0)    收藏  举报