🔥 🔥 🔥 Easy to understand | 💯 ✅ | In 4 languages 🔥 🔥 🔥
Intuition\n- The function maximizes happiness by selecting elements from a sorted list, taking into account diminishing returns as more elements are selected. I
# Intuition\n- The function maximizes happiness by selecting elements from a sorted list, taking into account diminishing returns as more elements are selected. It uses a strategy where high-value elements have a priority, ensuring that selections early on contribute the most happiness before penalties increase.\n\n# Approach\n- **Sort the List:** Start by sorting the happiness list in descending order to prioritize higher values for early selection.\n- **Initialization:** Set up counters for the number of selections (selected) and the total happiness score (happinessScore).\n- **Iterate with Condition:** For each happiness value, add to the score only if it\'s positive after subtracting the selection count, stopping if k elements are selected.\n- **Return the Result:** After processing up to k elements or exhausting the list, return the accumulated happiness score.\n\n# Complexity\n- Time complexity:\nO(NlogN)\n\n- Space complexity:\nO(1)\n\n# Code\n```Python []\nclass Solution:\n    def maximumHappinessSum(self, happiness: List[int], k: int) -> int:\n        happiness.sort(reverse=True)\n        happinessScore = 0\n\n        for turn, score in enumerate(happiness):\n            if k == turn:\n                return happinessScore\n            happinessScore += max(0, score - turn)\n\n        return happinessScore\n```\n```Java []\nclass Solution {\n    public long maximumHappinessSum(int[] happiness, int k) {\n        // Sort the array in ascending order\n        Arrays.sort(happiness);\n\n        // Reverse the array to get descending order\n        reverseArray(happiness);\n\n        int selected = 0;\n        long happinessScore = 0; // Changed to \'long\' to handle larger sums\n\n        // Iterate over the sorted happiness values\n        for (int score : happiness) {\n            if (selected == k) {\n                break; // Stop if \'k\' elements have been selected\n            }\n            // Calculate and add the adjusted happiness value if it\'s positive\n            happinessScore += Math.max(0, score - selected);\n            selected++;\n        }\n\n        return happinessScore;\n    }\n\n    // Helper method to reverse the array\n    private void reverseArray(int[] array) {\n        for (int i = 0, j = array.length - 1; i < j; i++, j--) {\n            int temp = array[i];\n            array[i] = array[j];\n            array[j] = temp;\n        }\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    long long maximumHappinessSum(vector<int>& happiness, int k) {\n        // Sort the vector in ascending order\n        std::sort(happiness.begin(), happiness.end());\n\n        // Reverse the vector to get descending order\n        std::reverse(happiness.begin(), happiness.end());\n\n        int selected = 0;\n        long long happinessScore = 0; // Use long long to handle larger sums\n\n        // Iterate over the sorted happiness values\n        for (int score : happiness) {\n            if (selected == k) {\n                break; // Stop if \'k\' elements have been selected\n            }\n            // Calculate and add the adjusted happiness value if it\'s positive\n            happinessScore += std::max(0, score - selected);\n            selected++;\n        }\n\n        return happinessScore;\n    }\n};\n```\n```Javascript []\n/**\n * @param {number[]} happiness\n * @param {number} k\n * @return {number}\n */\nvar maximumHappinessSum = function (happiness, k) {\n    // Sort the array in descending order using sort and a comparison function\n    happiness.sort((a, b) => b - a);\n\n    let selected = 0;\n    let happinessScore = 0;\n\n    // Iterate over the sorted happiness values\n    for (let score of happiness) {\n        if (selected === k) {\n            break; // Stop if \'k\' elements have been selected\n        }\n        // Calculate and add the adjusted happiness value if it\'s positive\n        happinessScore += Math.max(0, score - selected);\n        selected++;\n    }\n\n    return happinessScore;\n};\n```\n\n**Please Upvote**\n\n![DALL\xB7E 2024-05-08 00.50.08 - A visually appealing graphic design featuring the text \'Please Upvote. It makes me happy\' in vibrant, eye-catching colors. The background should inclu.webp](https://assets.leetcode.com/users/images/18000088-d49d-4fc9-8e7f-5d32ef02fe15_1715214723.5240114.webp)\n
['Array', 'Greedy', 'Sorting', 'C++', 'Java', 'Python3', 'JavaScript']