C++ simple O(nlogn) solution using priority queue


  • 0
    S
    class Solution {
    public:
        int hIndex(vector<int>& citations) {
            if (citations.empty()) return 0;
            
            priority_queue<int> pq; //max
            for (int i=0; i<citations.size(); i++) {
                pq.push(citations[i]);
            }
            
            int pages = 0;
            while (!pq.empty()) {
                int cur_c = pq.top();
                pq.pop();
                
                pages += 1;
                if (cur_c >= pages && (pq.empty() ||pages >= pq.top())) return pages;
            }
            
            return 0;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.