Skip to content

455. Assign Cookies 👍

  • Time: $O(\max(|\texttt{g}|\log |\texttt{g}|, |\texttt{s}|\log |\texttt{s}|))$
  • Space: $O(1)$
 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) {
    sort(begin(g), end(g));
    sort(begin(s), end(s));

    int i = 0;
    for (int j = 0; j < s.size() && i < g.size(); ++j)
      if (g[i] <= s[j])
        ++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 (int j = 0; i < g.length && j < s.length; ++j)
      if (g[i] <= s[j])
        ++i;

    return i;
  }
}
Back to top