Simple 3ms O(N log N) C++ Solution


  • 0
    C

    My approach:

    • Sort vector in ascending order - O(n log n)
    • Iterate backwards - O(n)
    • Increment h-index while citations are greater than h-index and iterator >= 0

    3ms runtime and O(N log N) time complexity.

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

Log in to reply
 

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