
😎풀이
children
: 아이의 배고픔 정도에 따라 오름차 순 정렬
cookie
: 쿠키의 포화량 기준 내림차 순 정렬
result
: 배부른 아이의 수
children
순회
4-1. cookie
를 순회하며 현재 쿠키로 아이를 배불리 먹일 수 있을 때까지 탐색
4-2. 먹일 수 있다면 정답 추가 후 다음 아이 탐색
4-3. 먹일 수 없다면 쿠키 추가 탐색
result
(배부른 아이의 수) 반환
function findContentChildren(g: number[], s: number[]): number {
const children = g.sort((a, b) => a - b)
const cookie = s.sort((a, b) => b - a)
let result = 0
for(const curChild of children) {
while(cookie.length) {
const curCookie = cookie.pop()
if(curCookie < curChild) continue
result++
break
}
}
return result
};