leetcode - 275. H-Index II

这篇具有很好参考价值的文章主要介绍了leetcode - 275. H-Index II。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Description

Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper and citations is sorted in ascending order, return the researcher’s h-index.

According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.

You must write an algorithm that runs in logarithmic time.

Example 1:

Input: citations = [0,1,3,5,6]
Output: 3
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had received 0, 1, 3, 5, 6 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, their h-index is 3.

Example 2:

Input: citations = [1,2,100]
Output: 2

Constraints:

n == citations.length
1 <= n <= 105
0 <= citations[i] <= 1000
citations is sorted in ascending order.

Solution

o ( n ) o(n) o(n) Solution

Iterate through the citations list from back to the front, every time compare the number of papers that have higher citations with the citation.

Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( 1 ) o(1) o(1)

o ( log ⁡ n ) o(\log n) o(logn) Solution

The sorted list and requirement actually give us a hint, usually it’s by binary search that we could reduce the time complexity to o ( log ⁡ n ) o(\log n) o(logn)

From the above solution, we could see that when going from the biggest citation to the smallest, the citation decrease while the number of the paper increases. So the key is to find the point where citation is smaller than the number of the paper. That is, to find the rightest index of the interval that citation is smaller than the number of the papers. Since we need to find the rightest index, we could discard the right half.

Time complexity: o ( log ⁡ n ) o(\log n) o(logn)
Space complexity: o ( 1 ) o(1) o(1)文章来源地址https://www.toymoban.com/news/detail-486255.html

Code

o ( n ) o(n) o(n) Solution

class Solution:
    def hIndex(self, citations: List[int]) -> int:
        res = 0
        paper_cnt = 0
        for i in range(len(citations) - 1, -1, -1):
            paper_cnt += 1
            res = max(res, min(paper_cnt, citations[i]))
        return res
        

o ( log ⁡ n ) o(\log n) o(logn) Solution

class Solution:
    def hIndex(self, citations: List[int]) -> int:
        res = 0
        left, right = 0, len(citations) - 1
        while left < right:
            mid = (left + right + 1) >> 1
            if len(citations) - mid < citations[mid]:
                right = mid - 1
            else:
                left = mid
        mid = (left + right + 1) >> 1
        if mid < len(citations) - 1:
            return max(min(len(citations) - mid, citations[mid]), min(len(citations) - mid - 1, citations[mid + 1]))
        return min(len(citations) - mid, citations[mid])

到了这里,关于leetcode - 275. H-Index II的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode:274. H 指数、275. H 指数 II(C++)

    目录 274. H 指数 题目描述: 实现代码与解析: 排序+暴力 275. H 指数 II 题目描述: 实现代码与解析: 二分 比较简单,不再写解析,注意二分的时候,r指针为n,含义为个数,而不是下标就行。         给你一个整数数组  citations  ,其中  citations[i]  表示研究者的第 

    2024年02月07日
    浏览(28)
  • [力扣146. LRU 缓存 ](https://leetcode.cn/problems/lru-cache/description/)

    力扣146. LRU 缓存 使用LinkedHashmap(HashMap的子类,能够记住插入数据的顺序). LRU是Lease Recently User的缩写,意思是最近 最少使用。比如设计一个文件缓存系统,每个文件有自己的大小和访问时间,文件缓存系统有总的大小,当往这个文件系统中放入新的文件时,如果发现超出文件

    2024年02月11日
    浏览(51)
  • TypeError: only integer scalar arrays can be converted to a scalar index

    报错信息: 类型错误,只有整型标量数组才能转换成标量索引,但一般问题都不在于你的索引是不是整数。这个报错一般会出现在你想使用一个索引列表去索引另一个列表,即诸如list[index_list]的形式,此时就会出现此报错,因为 index_list 为 List列表类型,不被允许;如果是数

    2024年02月11日
    浏览(67)
  • Leetcode 1325. Delete Leaves With a Given Value (二叉树后序遍历经典题)

    Delete Leaves With a Given Value Solved Medium Topics Companies Hint Given a binary tree root and an integer target, delete all the leaf nodes with value target. Note that once you delete a leaf node with value target, if its parent node becomes a leaf node and has the value target, it should also be deleted (you need to continue doing that until you cannot)

    2024年02月22日
    浏览(44)
  • LeetCode //189. Rotate Array

    Given an integer array nums , rotate the array to the right by k steps, where k is non-negative.   Example 1: Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] Example 2: Input: nums = [-1,-100,3,99],

    2024年02月13日
    浏览(38)
  • LeetCode //88. Merge Sorted Array

    You are given two integer arrays nums1 and nums2 , sorted in non-decreasing order, and two integers m and n , representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1

    2024年02月12日
    浏览(45)
  • LeetCode --- 1929. Concatenation of Array 解题报告

    Given an integer array  nums  of length  n , you want to create an array  ans  of length  2n  where  ans[i] == nums[i]  and  ans[i + n] == nums[i]  for  0 = i n  ( 0-indexed ). Specifically,  ans  is the  concatenation  of two  nums  arrays. Return  the array  ans . Example 1: Example 2:

    2024年02月09日
    浏览(48)
  • leetcode Median of Two Sorted Arrays

    Given two sorted arrays  nums1  and  nums2  of size  m  and  n  respectively, return  the median  of the two sorted arrays. The overall run time complexity should be  O(log (m+n)) . Example 1: Example 2: Constraints: nums1.length == m nums2.length == n 0 = m = 1000 0 = n = 1000 1 = m + n = 2000 -106 = nums1[i], nums2[i] = 106

    2023年04月08日
    浏览(39)
  • Leetcode array 704 27 189 121 380 238 134 13

    二分搜索: 判断条件是left 和 right 这里就要注意区间开闭的问题 因为需要运行时间时O(1),所以要加上map 删除的时候要把需要删除的变到最后,再直接pop掉 rand用法: 1.rand() 2. nums[rand() % nums.size()]; 3. left+rand() % (right-left)

    2024年02月09日
    浏览(32)
  • leetcode - 2149. Rearrange Array Elements by Sign

    You are given a 0-indexed integer array nums of even length consisting of an equal number of positive and negative integers. You should rearrange the elements of nums such that the modified array follows the given conditions: Every consecutive pair of integers have opposite signs. For all integers with the same sign, the order in which they were present in n

    2024年02月20日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包