H-Index II -- LeetCode

Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher‘s h-index.

According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."

For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 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, his h-index is 3.

Note: If there are several possible values for h, the maximum one is taken as the h-index.

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

思路:二分查找。

注意搜索的边界情况。

 1 class Solution {
 2 public:
 3     int hIndex(vector<int>& citations) {
 4         int left = 0, right = citations.size() - 1;
 5         while (left <= right) {
 6             int mid = left + (right - left) / 2;
 7             if (citations[mid] < citations.size() - mid)
 8                 left = mid + 1;
 9             else if (mid > left && citations[mid - 1] >= citations.size() - (mid - 1))
10                 right = mid - 1;
11             else return citations.size() - mid;
12         }
13         return 0;
14     }
15 };
时间: 2024-10-11 06:17:37

H-Index II -- LeetCode的相关文章

Pascal&#39;s Triangle II Leetcode java

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 题解: 为了达到O(k)的空间复杂度要求,那么就要从右向左生成结果.相当于你提前把上一行的计算出来,当前行就可以用上一次计算出的结果计算了

Pascal&#39;s triangle II Leetcode Python

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 这题和前面一题的区别在于不用返回所有解,只需要返回index对应行就行.做法和前面的一样定义一个currow 和prerow class Solu

Jump Game II leetcode java

题目: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of j

Pascal&#39;s Triangle II —LeetCode

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 这道题倒是不难,有个有意思的地方是可以优化到O(k)的空间复杂度.下面先上O(k^2)的算法. @Test public List<Integer>

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

Permutations II leetcode java

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一

Combination Sum II leetcode java

题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

Reverse Linked List II leetcode java

题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n

Word Ladder II leetcode java

题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: start =

Populating Next Right Pointers in Each Node II leetcode java

题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example, Given the following bina