azl397985856/leetcode

二分法的套路

azl397985856 opened this issue · 1 comments

  • 274 H 指数

经典问题和模型

class Solution:
    def hIndex(self, citations: List[int]) -> int:
        def test(mid):
            cnt = 0
            for citation in citations:
                if citation >= mid: cnt += 1
                if cnt >= mid: return True
            return False
        if not citations: return 0
        l, r = 0, max(citations)

        while l <= r:
            mid = (l + r) // 2
            if test(mid):
                l = mid + 1
            else:
                r = mid - 1
        return r
stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.