4ms Simple C++ Solution


  • 0
    G

    My solution first sort the input, then enumerate elements from the last one(most cititations) to the first one to get the answer. The time complexity of my solution is O(n logn). An optimization is that we can break the loop if cititation is small than h, however it runs in 4ms even without the optimization.

    class Solution {
    public: int hIndex(vector<int>& citations) {
    	int h = 0;
    	int n = citations.size();
    	sort(citations.begin(), citations.end());
    
    	for(int i=1; i<=citations.size()&&citations[n-i]>=h; ++i){
    		h = max(h, min(i, citations[n-i]));
    	}
    
    	return h;
    }
    

    };


Log in to reply
 

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