알고리즘

LeetCode : : 455번 Assign Cookies - C++ 풀이

green333 2021. 12. 31. 01:43
728x90
SMALL

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

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//[1, 2, 3], [1, 1] : 1
//[10, 9, 8, 7], [5, 6, 7, 8] : 2
//[1, 2, 3], [3] : 1
class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        int answer = 0
        int cookieIdx = 0;
        
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());
        
        for(int i = 0; i < g.size() ; i++){
            
            while(cookieIdx < s.size()){
                if(g[i] <= s[cookieIdx]){
                    cookieIdx++;
                    answer++;
                    break;
                }    
                cookieIdx++;  
                if(cookieIdx >= s.size()) return answer;
            }
            
        }
        return answer;
    }     
};
 
cs
vector sorting 하는 것! vector 사용 익숙해지기, 문제 이해 빠르게하기!



 

LIST