JavaScript version


  • 0
    Z
    var hIndex = function(citations) {
        var possible = [];
      var len = citations.length;
      if (len === 0) return 0;
    
      // possible array length = citations.length + 1, 0 - citations.length
      possible.length = len + 2;
      // fill with 0
      possible = possible.join('-').split('').map(function() {
        return 0;
      });
    
      for (var i = 0; i < len; i ++) {
        var val = citations[i];
        var idx = val > len ? len : val;
    
        possible[idx] += 1;
      }
    
      var result = 0;
      for (var k = len; k >= 0; k --) {
        result += possible[k];
        if (result >= k) {
          return k;
        }
      }
    };
    

    Repo here, Javascript version, WIP: https://github.com/zhiyelee/leetcode


  • 0
    Z

    Thanks diaoZhaTian


Log in to reply
 

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