C++ Sort and Merge, O(N*Log(N)) Time O(1) Space


  • 0
    M
    class Solution {
    public:
        int findContentChildren(vector<int>& g, vector<int>& s) {
            sort(g.begin(), g.end());
            sort(s.begin(), s.end());
            int result = 0, pg = 0, ps = 0;
            while (pg < g.size() && ps < s.size()) {
                while (ps < s.size() && s[ps] < g[pg]) ++ps;
                if (ps >= s.size()) break;
                ++result; ++ps; ++pg;
            }
            return result;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.