Created
December 25, 2025 09:04
-
-
Save SuryaPratapK/d320c7dff2dd100a039b74de8208a803 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| using ll = long long; | |
| public: | |
| long long maximumHappinessSum(vector<int>& happiness, int k) { | |
| ll sum = 0; | |
| priority_queue<int> maxheap(happiness.begin(),happiness.end()); | |
| int decrease = 0; | |
| while(k--){ | |
| sum += maxheap.top(); | |
| sum -= min(decrease,maxheap.top()); | |
| maxheap.pop(); | |
| decrease++; | |
| } | |
| return sum; | |
| } | |
| }; | |
| /* | |
| //JAVA | |
| import java.util.PriorityQueue; | |
| import java.util.Collections; | |
| class Solution { | |
| public long maximumHappinessSum(int[] happiness, int k) { | |
| long sum = 0L; | |
| PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); | |
| for (int h : happiness) maxHeap.add(h); | |
| int decrease = 0; | |
| while (k-- > 0 && !maxHeap.isEmpty()) { | |
| int top = maxHeap.poll(); | |
| sum += top; | |
| sum -= Math.min(decrease, top); | |
| decrease++; | |
| } | |
| return sum; | |
| } | |
| } | |
| #Python | |
| import heapq | |
| from typing import List | |
| class Solution: | |
| def maximumHappinessSum(self, happiness: List[int], k: int) -> int: | |
| # Use a max-heap via negation | |
| max_heap = [-h for h in happiness] | |
| heapq.heapify(max_heap) | |
| total = 0 | |
| decrease = 0 | |
| while k > 0 and max_heap: | |
| top = -heapq.heappop(max_heap) | |
| total += top | |
| total -= min(decrease, top) | |
| decrease += 1 | |
| k -= 1 | |
| return total | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment