Leetcode: 贪心算法
admin
2024-01-29 20:51:08

leetcode 贪心算法汇总

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 g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], 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.

Example 1:

Input: g = [1,2,3], s = [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.
 class Solution {public int findContentChildren(int[] g, int[] s) {Arrays.sort(g);Arrays.sort(s);int a_pointer = 0;int b_pointer = 0;while (a_pointer < g.length && b_pointer < s.length) {if (g[a_pointer] <= s[b_pointer]) {a_pointer++;b_pointer++;} else {b_pointer++;}}return a_pointer;}
}

相关内容

热门资讯

同样卖奶茶,为何古茗赚了15亿... 出品/联商专栏 撰文/老刀 编辑/蔡建桢 今年上半年,蜜雪、古茗、霸王茶姬、茶百道、沪上阿姨全部盈利...
现烤散装走俏、新口味亮相、健康... 中秋佳节临近,成都月饼市场已全面开启“上新季”。记者连日来走访成都多家商超看到,月饼专区占据卖场核心...
散装月饼靠性价比当上“人气王” 华润超市货架上的散装月饼。 深圳报业集团记者 欧阳莹 摄 深圳晚报讯 (深圳报业集团记者 欧阳莹) ...
原创 9... 九月初秋,燥气渐起,容易咽喉干痒、口鼻缺水、干咳烦闷。多喝清润汤水,滋养肺阴,补足身体津液,不寒不腻...
原创 生... 山药是秋季深藏不露的养生食材,也是为数不多 生用补肾、熟用健脾 的平价好物。生山药滋阴补肾、固护肾精...