Problem Solving/리트코드(leetcode)
[Leetcode/C++] 455. Assign Cookies
높은곳에영광
2022. 1. 16. 23:12
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