반응형

https://leetcode.com/problems/assign-cookies/description/

 

Assign Cookies - LeetCode

Can you solve this real interview question? Assign Cookies - Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor g[i], which is the minimum size o

leetcode.com

 

g와 s를 오름차순으로 정렬하고, 탐색되는 아이가 수용 가능한 쿠키를 만나면 다음 아이를 탐색해줍니다.

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());

        int i = 0, j = 0;
        while(i < g.size()) {
            while(j < s.size() && g[i] > s[j]) j++;
            if(j == s.size()) break;
            i++, j++;
        }

        return i;
    }
};
반응형

+ Recent posts