leetcode 455. 分发饼干

贪心法的简单应用,排序完后,每次都把最小的分给最小胃口的孩子。

开始几行是c++的输入输出加速,可以不必理会,只看solution 里的内容即可。

static int x = [](){

    ios::sync_with_stdio(false);

    cin.tie(0);

    return 0;

}();

class Solution {

public:

    int findContentChildren(vector<int>& g, vector<int>& s) {

        if(s.size() == 0)

            return 0;

        sort(g.begin(),g.end());

        sort(s.begin(),s.end());

        int ret = 0, j = 0;;

        for(int i = 0; i < s.size(); i++){

            if(j < g.size()){

                if(g[j] <= s[i]){

                    ret++;

                    j++;

                }

            }

            else

                break;

        }

        return ret;

    }

};


运行时间:20ms

猜你喜欢

转载自blog.csdn.net/torch_man/article/details/80559001