455. Assign Cookies

 

Intuition

sort all the array before asigning

Approach

compare s with g with two pointer and return the poiter of g

Complexity

  • Time complexity: O(n)
  • Space complexity: O(1)

Code

JavaScript
/**
 * @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

Popular posts from this blog

3Sum Closest: O(n²) Two-Pointer Magic šŸš€ leetcode: 16

Kadane's Algorithm: Maximum Subarray Problem - LeetCode(53) Solution

LeetCode Problem: Implement pow(x, n) leetcode:- 50