The Assign Cookies solutions are below.
15 Jun 2024•Easy•C++ / Python / TypeScript
Assign Cookies
C++, Python and TypeScript solutions for Assign Cookies.
Solution files
Python•assign-cookies/synced-solution.py
def findContentChildren(g, s):
g.sort()
s.sort()
child = 0
cookie = 0
while child < len(g) and cookie < len(s):
if s[cookie] >= g[child]:
child += 1
cookie += 1
return child
C++•assign-cookies/synced-solution.cpp
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int child = 0, cookie = 0;
while (child < g.size() && cookie < s.size()) {
if (s[cookie] >= g[child]) {
child++;
}
cookie++;
}
return child;
}
};
TypeScript•assign-cookies/synced-solution.ts
function findContentChildren(g: number[], s: number[]): number {
g.sort((a, b) => a - b);
s.sort((a, b) => a - b);
let child = 0, cookie = 0;
while (child < g.length && cookie < s.length) {
if (s[cookie] >= g[child]) {
child++;
}
cookie++;
}
return child;
}