class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int count = 0;
for(int i = 0 ; i < g.size(); i++) {
for(int j = 0 ; j < s.size(); j++) {
if(g[i] <= s[j]) {
count++;
s[j] = -5;
break;
}
}
}
return count;
}
};
그리디 알고리즘을 적용해서 가장 작은 친구부터 가장 적은 쿠키수로 충족시켜나아가며 가능한 횟수를 최대한으로 계산하였습니다.
문제: https://leetcode.com/problems/assign-cookies/
Assign Cookies - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
'Problem Solving > 리트코드(leetcode)' 카테고리의 다른 글
Summary Ranges (0) | 2022.01.16 |
---|---|
Single Number (0) | 2022.01.16 |
Count Primes (0) | 2022.01.16 |
[Leetcode/C++] 66. Plus One (0) | 2022.01.16 |
Pascal's Triangle (0) | 2022.01.16 |