🏆💢💯 Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅💥🔥💫Explained☠💥🔥 Beats 💯
Intuition\n\n\n\n\n\nC++ []\nclass Solution {\npublic:\n    long long maximumHappinessSum(vector<int>& happiness, int k) {\n        long long cnt = 0;\n        
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/2de357f2-3916-48c0-9ce0-c7347b99c4ce_1715227399.0655422.jpeg)\n![Screenshot 2024-05-09 093208.png](https://assets.leetcode.com/users/images/404bccac-91f2-47c2-9770-a90ec04e5cbe_1715227409.0444713.png)\n![Screenshot 2024-05-09 093222.png](https://assets.leetcode.com/users/images/4596f269-d8a7-4cf6-9029-85fd4756be47_1715227414.6215477.png)\n![Screenshot 2024-05-09 093236.png](https://assets.leetcode.com/users/images/992ff71d-5720-40ca-8709-cf651a31e2f0_1715227419.5937421.png)\n![Screenshot 2024-05-09 093248.png](https://assets.leetcode.com/users/images/b01ab070-38ff-4a99-a89f-fd7b271dc99f_1715227424.2885182.png)\n```C++ []\nclass Solution {\npublic:\n    long long maximumHappinessSum(vector<int>& happiness, int k) {\n        long long cnt = 0;\n        sort(happiness.begin(), happiness.end(), greater<int>());\n        cnt = (long long)happiness[0];\n        for (int i = 1; i < k and happiness[i] - i > 0; ++i)\n            cnt += (long long)happiness[i] - i;\n        return cnt;\n    }\n};\n```\n```python3 []\nclass Solution:\n    def maximumHappinessSum(self, happiness: List[int], k: int) -> int:\n        cnt = 0\n        happiness.sort(reverse = True)\n        cnt = happiness[0]\n        for i in range(1, k):\n            if happiness[i] - i <= 0:\n                break\n            cnt += happiness[i] - i\n        return cnt\n```\n```C []\nint cmp(const void *a, const int *b){\n    return *((int*)b) - *((int*)a);\n    }\nlong long maximumHappinessSum(int* happiness, int happinessSize, int k) {\n    long long int cnt = 0;\n    qsort(happiness, happinessSize, sizeof(int), cmp);\n    cnt = (long long)happiness[0];\n    for(int i = 1; i < k && (happiness[i] - i) > 0; ++i)\n        cnt += (long long)happiness[i] - i;\n    return cnt;\n}\n```\n```Java []\nclass Solution {\n    public long maximumHappinessSum(int[] happiness, int k) {\n        long cnt = 0;\n        Integer[] happy = new Integer[happiness.length];\n        for (int i = 0; i < happiness.length; i++)\n            happy[i] = happiness[i];\n        Arrays.sort(happy, Collections.reverseOrder());\n        cnt = (long) happy[0];\n        for (int i = 1; i < k && (happy[i] - i) > 0; i++)\n            cnt += (long) happy[i] - i;\n        return cnt;\n    }\n}\n```\n```python []\nclass Solution(object):\n    def maximumHappinessSum(self, happiness, k):\n        """\n        :type happiness: List[int]\n        :type k: int\n        :rtype: int\n        """\n        h = sorted(happiness, reverse = True)\n        res = 0\n        for i in range(k):\n            if h[i] <= i:\n                return res\n            res += h[i] - i\n        return res\n```\n\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(nlogn)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n     O(1)\n# Code\n```\nint cmp(const void *a, const int *b){\n    return *((int*)b) - *((int*)a);\n    }\nlong long maximumHappinessSum(int* happiness, int happinessSize, int k) {\n    long long int cnt = 0;\n    qsort(happiness, happinessSize, sizeof(int), cmp);\n    cnt = (long long)happiness[0];\n    for(int i = 1; i < k && (happiness[i] - i) > 0; ++i)\n        cnt += (long long)happiness[i] - i;\n    return cnt;\n}\n```\n# Please upvote if it\'s useful for you..\n![th.jpeg](https://assets.leetcode.com/users/images/5d2a3190-157f-4d92-9242-4293a16f803e_1715227545.7892096.jpeg)\n
['Array', 'Greedy', 'C', 'Sorting', 'Python', 'C++', 'Java', 'Python3']