Posted on

Description

Submission

class Solution {
public:
    int fillCups(vector<int>& amount) {
        priority_queue<int> pq {amount.begin(), amount.end()};
        
        if(pq.top() == 0) return 0;
        
        int ret = 0;
        while(pq.size() >= 2) {
            int a = pq.top();
            pq.pop();
            int b = pq.top();
            pq.pop();
            
            if(a == 0) break;
            
            a--;
            b--;
            ret++;
            
            if(a > 0) pq.push(a);
            if(b > 0) pq.push(b);
        }
        if(!pq.empty()) ret += pq.top();
        
        return ret;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *