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; } };
时间: 2024-10-01 06:52:58