You can also model this as bipartite matching: child connects to cookie if and only if . Run Kuhn's algorithm to find the maximum matching between children and cookies.
This approach is worst case, which is slower than the greedy solution.
But it demonstrates that matching can solve problems where greedy also works. For this specific problem, use greedy. Know the matching alternative exists.
Space complexity is for the data structures used.