455. Assign Cookies
View on LeetCode ↗Solution:
C++
class Solution {public: int findContentChildren(vector<int>& g, vector<int>& s) { sort(g.begin(), g.end()); sort(s.begin(), s.end()); int count = 0; int c = 0; int p = 0; while (c < s.size() && p < g.size()) { if (s[c] >= g[p]) { count++; c++; p++; } else { c++; } } return count; }};
this was a very simple two pointer greedy problem. I was doing too much by trying to a hardcoded indexing shift. much easier to just do two pointer. sort first. pointer for cookie, pointer for child, move both and have the count increase if requirement is ment, if not move cookie. I was hesitant to doint this before because I count figure out the possible end solutions and was trying to for loop the cookies vector. but that was too constrained. two pointers is really easy.