Assign Cookies Solutions in C++
Number 455
Difficulty Easy
Acceptance 49.9%
Link LeetCode
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// 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 ++;}elsegi ++;}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;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/assign-cookies/description//// Author : liuyubobobo/// Time : 2018-01-17#include <iostream>#include <vector>#include <algorithm>using namespace std;/// Greedy Algorithm/// Serve least 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());sort(s.begin(), s.end());int gi = 0, si = 0;int res = 0;while(gi < g.size() && si < s.size()){if(s[si] >= g[gi]){res ++;si ++;gi ++;}elsesi ++;}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;}