Skip to content

Commit 7c5cf4c

Browse files
committed
Runtime: 28 ms (Top 16.33%) | Memory: 18.00 MB (Top 6.11%)
1 parent 8e21bd5 commit 7c5cf4c

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
// Runtime: 28 ms (Top 16.33%) | Memory: 18.00 MB (Top 6.11%)
2+
13
class Solution {
24
public:
35
int findContentChildren(vector<int>& g, vector<int>& s) {
4-
int n=g.size();
5-
int m=s.size();
6-
if(n==0 or m==0)return 0;
7-
sort(g.begin(),g.end());
8-
sort(s.begin(),s.end());
9-
int i=0,j=0;
10-
while(i<m and j<n)
11-
{
12-
if(s[i]>=g[j])
13-
j++;
14-
i++;
6+
sort(g.begin(), g.end()); // sort the children's greed factors in non-decreasing order
7+
sort(s.begin(), s.end()); // sort the cookie sizes in non-decreasing order
8+
int contentChildren = 0;
9+
int i = 0; // pointer to the current child's greed factor
10+
int j = 0; // pointer to the current cookie size
11+
while (i < g.size() && j < s.size()) {
12+
if (s[j] >= g[i]) { // if the current cookie can satisfy the current child's greed factor
13+
contentChildren++;
14+
i++;
15+
}
16+
j++;
1517
}
16-
return j;
18+
return contentChildren;
1719
}
18-
};
20+
};

0 commit comments

Comments
 (0)