内容简介:Link:思路:1,暴力破解,挨个枚举,时间复杂度O(n!)
Link: https://leetcode.com/problems/assign-cookies/
思路:
1,暴力破解,挨个枚举,时间复杂度O(n!)
2, 排序后贪心O(n)
两个数组 排序 后,以children的为基点遍历,与此同时,对应比较cookies数组中相应的值,如果比cookies数组中小,则跳过。
代码:
function sortNumber(a, b) {
return a - b;
}
var findContentChildren = function(g, s) {
const sortedChildren = g.sort(sortNumber);
const sortedCookies = s.sort(sortNumber);
let i = 0;
let count = 0;
for (var key in sortedChildren) {
while (sortedChildren[key] > sortedCookies[i] && i < sortedCookies.length) {
i++;
}
if (i < sortedCookies.length) {
count++;
i++;
}
}
return count;
};
findContentChildren([10, 9, 8, 7], [5, 6, 7, 8]);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Weaving the Web
Tim Berners-Lee / Harper Paperbacks / 2000-11-01 / USD 15.00
Named one of the greatest minds of the 20th century by Time , Tim Berners-Lee is responsible for one of that century's most important advancements: the world wide web. Now, this low-profile genius-wh......一起来看看 《Weaving the Web》 这本书的介绍吧!