Detailed Explanation😴|| Priority Queue + Greedy + Sort || O(n.logn) + O(k.logn)🔥
Intuition (Approch: MaxHeap + Greedy)\nGiven our problem\'s requirement to maximize total happiness by selecting the largest element available at each turn, the
# Intuition (Approch: MaxHeap + Greedy)\nGiven our problem\'s requirement to maximize total happiness by selecting the largest element available at each turn, the selection of an appropriate data structure holds significant importance. The max heap data structure proves to be exceptionally suitable for this scenario. By arranging all happiness scores into a max heap, we guarantee that the largest element consistently resides at the top, ensuring swift accessibility. This attribute seamlessly complements the strategy of our greedy algorithm, which aims to choose the highest happiness value attainable at each iteration.\n\nInitially, we construct a max heap utilizing the values from the happiness array. Subsequently, for each of the k turns, we extract the maximum value from the heap. Following this, we modify this value by subtracting the number of completed turns. This adjustment compensates for the diminishing happiness of the remaining unselected children. Ultimately, we incorporate this adjusted value into our cumulative happiness tally.\n\nBy retaining its inherent greedy approach of selecting the highest happiness values sequentially, the employment of a heap data structure markedly enhances efficiency in contrast to sorting the happiness array.\n\n# Complexity\n- Time complexity: O(n.logn) + O(k.logn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution {\n    public long maximumHappinessSum(int[] happiness, int k) {\n        int turns = 0, n = happiness.length;\n        long happinessSum = 0;\n        Integer[] happinessArray = new Integer[n];\n        for(int i = 0; i < n; i++) {\n            happinessArray[i] = happiness[i];\n        }\n        Arrays.sort(happinessArray, Collections.reverseOrder());\n        for(int i = 0; i < k; i++){\n            happinessSum += Math.max(happinessArray[i] - turns, 0);\n            turns++;\n        }\n\n        return happinessSum;\n    }\n}\n```\n---\n![Upvote.png](https://assets.leetcode.com/users/images/ff77f6a8-3a4a-425b-bc21-548b57019d46_1715220074.2853308.png)\n
['Array', 'Greedy', 'Sorting', 'Heap (Priority Queue)', 'Java']