首页 > 其他 > 详细

[Leetcode] Binary search--275 H-Index

时间:2017-06-10 12:03:03      阅读:241      评论:0      收藏:0      [点我收藏+]

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?

 Solution:

 # this is could also be solved used sorting or hash method reference as problem 274. H-index

here we use binary search
inspired from sorting method c[i] < i+1;
when citations c is in ascending order, the idea is find the minimum index c[i] >= len(c) -i

技术分享
        l = 0
        h = len(citations)-1
        while (l <= h):
            mid = (l+h)/2
            if citations[mid] == len(citations)-mid:
               return len(citations)-mid
            elif citations[mid] < len(citations)-mid:
                l = mid+1
            else:
                h = mid-1
        
        return len(citations)-l
View Code

 

 

[Leetcode] Binary search--275 H-Index

原文:http://www.cnblogs.com/anxin6699/p/6978068.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!