Skip to content

455. Assign Cookies 👍

  • Time: $O(\texttt{sort}(g) + \texttt{sort}(s))$
  • Space: $O(\texttt{sort}(g) + \texttt{sort}(s))$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  int findContentChildren(vector<int>& g, vector<int>& s) {
    ranges::sort(g);
    ranges::sort(s);

    int i = 0;
    for (const int cookie : s)
      if (i < g.size() && g[i] <= cookie)
        ++i;

    return i;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
  public int findContentChildren(int[] g, int[] s) {
    Arrays.sort(g);
    Arrays.sort(s);

    int i = 0;
    for (final int cookie : s)
      if (i < g.length && g[i] <= cookie)
        ++i;

    return i;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution:
  def findContentChildren(self, g: list[int], s: list[int]) -> int:
    g.sort()
    s.sort()

    i = 0
    for cookie in s:
      if i < len(g) and g[i] <= cookie:
        i += 1

    return i