leetcode 455 Assign Cookies

python;

class Solution:
    def findContentChildren(self, g, s):
        """
        :type g: List[int]
        :type s: List[int]
        :rtype: int
        """
        ans = 0
        g.sort(reverse=True)
        s.sort(reverse=True)
        i=j=0
        while i < len(g) and j < len(s):
            if g[i] <= s[j]:
                i += 1
                j += 1
                ans += 1
            else:
                i += 1
        return ans

c++:

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        int ans = 0;
        int i = 0, j = 0;
        sort(g.begin(),g.end());
        sort(s.begin(),s.end());
        while(i<g.size() && j<s.size()){
            if(g[i]<=s[j]){
                ans++;
                i++;
                j++;
            }
            else if(g[i]>s[j]) j++;
        }
        return ans;
    }
};


猜你喜欢

转载自blog.csdn.net/mrxjh/article/details/80528659