반응형
https://leetcode.com/problems/assign-cookies/description/
g와 s를 오름차순으로 정렬하고, 탐색되는 아이가 수용 가능한 쿠키를 만나면 다음 아이를 탐색해줍니다.
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int i = 0, j = 0;
while(i < g.size()) {
while(j < s.size() && g[i] > s[j]) j++;
if(j == s.size()) break;
i++, j++;
}
return i;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 2870. Minimum Number of Operations to Make Array Empty (0) | 2024.01.04 |
---|---|
LeetCode 2610. Convert an Array Into a 2D Array With Conditions (0) | 2024.01.02 |
LeetCode 1624. Largest Substring Between Two Equal Characters (1) | 2023.12.31 |
LeetCode 1897. Redistribute Characters to Make All Strings Equal (0) | 2023.12.30 |
LeetCode 1335. Minimum Difficulty of a Job Schedule (0) | 2023.12.29 |