File tree 1 file changed +15
-13
lines changed
scripts/algorithms/A/Assign Cookies
1 file changed +15
-13
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 28 ms (Top 16.33%) | Memory: 18.00 MB (Top 6.11%)
2
+
1
3
class Solution {
2
4
public:
3
5
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 ++;
15
17
}
16
- return j ;
18
+ return contentChildren ;
17
19
}
18
- };
20
+ };
You can’t perform that action at this time.
0 commit comments