forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
53 lines (43 loc) · 1.24 KB
/
main.cpp
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/// Source : https://leetcode.com/problems/assign-cookies/description/
/// Author : liuyubobobo
/// Time : 2017-11-19
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/// Greedy Algorithm
/// Serve most greedy children first
/// Time Complexity: O(nlogn)
/// Space Complexity: O(1)
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end(), greater<int>());
sort(s.begin(), s.end(), greater<int>());
int gi = 0, si = 0;
int res = 0;
while(gi < g.size() && si < s.size()){
if(s[si] >= g[gi]){
res ++;
si ++;
gi ++;
}
else
gi ++;
}
return res;
}
};
int main() {
int g1[] = {1, 2, 3};
vector<int> gv1(g1, g1 + sizeof(g1)/sizeof(int));
int s1[] = {1, 1};
vector<int> sv1(s1, s1 + sizeof(s1)/sizeof(int));
cout << Solution().findContentChildren(gv1, sv1) << endl;
int g2[] = {1, 2};
vector<int> gv2(g2, g2 + sizeof(g2)/sizeof(int));
int s2[] = {1, 2, 3};
vector<int> sv2(s2, s2 + sizeof(s2)/sizeof(int));
cout << Solution().findContentChildren(gv2, sv2) << endl;
return 0;
}