C++ O(N) time O(N) space solution


  • 0
    U
    class Solution {
    public:
        int hIndex(vector<int>& citations) {
            vector<int> check(citations.size(), 0);
            int t = 0;
            int h = 0;
            
            for (int i = 0; i < citations.size(); i++) {
                if (citations[i] < citations.size()) {
                    check[citations[i]]++;
                    t++;
                }
            }
            if (t == 0)
                return citations.size();
            t = citations.size() - t;
            for (int i = citations.size() - 1; i >= 0; i--) {
                t += check[i];
                h = max(h, min(t, i));
            }
            return h;
        }
    };

Log in to reply
 

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