It is a trap that the cookies cannot be divided


  • 0
    P

    According to my original code, the input [1,2,3] [3] should receive an output 2 because 3=1+2,however it does not
    work and I change my code in the end.

    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;
    int a=g.size();
    int b=s.size();
    int cnt=0;
    while(i<a&&j<b){
    if(g[i]<=s[j]){
    cnt++;
    //s[j]-=g[i];
    i++;
    j++;
    }
    else{
    j++;
    }
    }
    return cnt;
    }
    };


Log in to reply
 

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