本文主要是介绍每日一题 274. H 指数(中等),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
先讲一下自己的复杂的写法
- 第一眼最大最小值问题,直接在0和最大被引次数之间二分找答案
- 先排序,再二分,,,
正解:
- 排序得到 citations 的递减序列,通过递增下标 i 遍历该序列
- 显然只要排序后的 citations[i] >= i + 1,那么 h 至少是 i + 1,由于 citations[i] 递减,i 递增,所以遍历过程中必有交点,也就是答案
class Solution:def hIndex(self, citations: List[int]) -> int:if len(citations) == 0:return 0citations.sort()l, r = 0, citations[-1]while l < r:mid = (l + r) >> 1t = len(citations) - bisect_left(citations, mid)if t >= mid:l = mid + 1if t < mid:r = midt = len(citations) - bisect_left(citations, r)return r if t >= r else l - 1
class Solution:def hIndex(self, citations: List[int]) -> int:sorted_citation = sorted(citations, reverse = True)h = 0; i = 0; n = len(citations)while i < n and sorted_citation[i] > h:h += 1i += 1return h
这篇关于每日一题 274. H 指数(中等)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!