FAST 100% | MEMORY EFFICIENT | GREEDY - BEGINNERS
Intuition\n\nThe goal is to optimize the overall happiness score by picking children with the greatest happiness levels. A sensible tactic is to give preference
# Intuition\n\nThe goal is to optimize the overall happiness score by picking children with the greatest happiness levels. A sensible tactic is to give preference to those children with the highest happiness ratings initially, given that the happiness of those not chosen diminishes over time.\n\nBy prioritizing children with the highest happiness ratings from the start, we secure the highest possible happiness sum before it possibly diminishes. This method focuses on maximizing immediate happiness gains by securing the highest happiness scores early in the process.\n\n# Complexity\n- Time complexity:$$O(n*log(n))$$ -->\n\n- Space complexity:$$O(1)$$\n\n# Code\n```\nclass Solution {\npublic:\n    long long maximumHappinessSum(vector<int>& h, int k) {\n        long long ans=0;\n        sort(h.begin(),h.end(),greater<int>());\n        for(int i=0; i<k; i++){\n            ans+=max(h[i]-i,0);\n        }\n        return ans;\n    }\n};\n```
['Greedy', 'Sorting', 'C++', 'Java']