LeetCode Solutions

455. Assign Cookies

Time: $O(\max(|\texttt{g}|\log |\texttt{g}|, |\texttt{s}|\log |\texttt{s}|))$

Space: $O(1)$

			

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;
  }
};
			

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;
  }
}