455. Assign Cookies
Intuition
Approach
Complexity
- Time complexity: O(n)
- Space complexity: O(1)
sort all the array before asigning
compare s with g with two pointer and return the poiter of g
/**
* @param {number[]} g
* @param {number[]} s
* @return {number}
*/
var findContentChildren = function(g, s) {
g = g.sort((a,b) => a-b);
s = s.sort((a,b) => a-b);
// console.log(g);
// console.log(s);
let i = 0, j = 0;
while(i <= j && j < s.length){
if(s[j] >= g[i]){
i++;
}
j++;
}
return i;
};
Comments
Post a Comment