Recursive Javascript Solution with Sort


  • 0
    R

    Javascript Recursive Solution with Sort. Assuming Javascript's native sort uses a sort with Big O >= merge sort, we'd have O(N logN) Time with O(1) Space. Always open to suggestions for improvement and clarification on my Big O.

    var hIndex = function(citations) {
        if (citations.length === 0) {
            return 0;
        }
        
        if (citations.length === 1) {
            return citations[0] === 0 ? 0 : 1;
        }
        
        const confirmHIndex = (arr, hIndex) => {
            if (arr[hIndex - 1] >= hIndex) {
                return hIndex;
            }
            
            if (hIndex === 0) {
                return 0;
            }
            
            return confirmHIndex(arr, hIndex - 1);
        }
        
        return confirmHIndex(citations.sort((a, b) => b - a), citations.length);
    };

Log in to reply
 

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