275. H-Index II

            }
     /*
      * 275. H-Index II
      * 1.3 by Mingyang
      * 在Sort里面有线性解法的
      * 输入数组是有序的,让我们在O(log n)的时间内完成计算,
      * 我们最先初始化left和right为0和数组长度len-1,然后取中间值mid,
      * 比较citations[mid]和len-mid做比较,如果前者大,则right移到mid之前,
      * 反之right移到mid之后,终止条件是left>right,最后返回len-left即可
      * for paper[m]. there should be at least (len – m) papers with citations >= citations[m]
      */
     public static int hIndex(int[] citations) {
            if (citations == null || citations.length == 0) {
                return 0;
            }

            int low = 0;
            int high = citations.length - 1;
            int len = citations.length;
            while (low <= high) {
                int mid = (low + high)/2;
                if (citations[mid] == len - mid) return len - mid;      //第k个数等于k,绝配----len-mid表示这个数从大到小排在第几位
                else if (citations[mid] > len - mid) high = mid - 1;
                else low = mid + 1;
            }
            return len - low;
        }
     //孟严的方法
     public int hIndex2(int[] citations) {
            int len=citations.length;
            if(len==0)
             return 0;
            int left=0,right=len-1;
            while(left<=right)
            {
                int mid=left+(right-left)/2;
               // 0 4 5 6 7   5>=3
                 //4>=4
                if(citations[mid]>=len-mid) //--------mid=2
                {
                    if(mid==0||citations[mid-1]<len-mid+1)
                        return len-mid;
                    else
                        right=mid-1;  // 0 4
                        //
                }
                else
                 left=mid+1;
            }
            return 0;
     }
时间: 2024-12-30 04:30:39

275. H-Index II的相关文章

[LeetCode] 275. H-Index II H指数 II

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? Hint: Expected runtime complexity is in O(log n) and the input is sorted. 274. H-Index H指数 的拓展.输入的数组是有序的,让我们优化算法.提示(现在题目中没有提示了):O(logn

LintCode &quot;Permutation Index II&quot; !

Simply a variation to "Permutation Index". When calculating current digit index, we consider duplicated case. Again, similar as "Digit Counts", it is another counting problem and stil digit by digit. And please note: we can use Fenwick

lintcode-medium-Permutation Index II

Given a permutation which may contain repeated numbers, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1. Example Given the permutation [1, 4, 2, 2], return 3. public class Sol

leet

# 题名1 两数之和    2 两数相加    3 无重复字符的最长子串    4 寻找两个有序数组的中位数    5 最长回文子串    6 Z 字形变换    7 整数反转    8 字符串转换整数 (atoi)    9 回文数    10 正则表达式匹配    11 盛最多水的容器    12 整数转罗马数字    13 罗马数字转整数    14 最长公共前缀    15 三数之和    16 最接近的三数之和    17 电话号码的字母组合    18 四数之和    19 删除链表

Permutation Index I &amp; II

Given a permutation which contains no repeated number, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1. Example Given [1,2,4], return 1. 分析:http://www.cnblogs.com/EdwardLiu/p/

[LeetCode] 274. H-Index H指数

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 a

FFmpeg的H.264解码器源代码简单分析:熵解码(Entropy Decoding)部分

本文分析FFmpeg的H.264解码器的熵解码(Entropy Decoding)部分的源代码.FFmpeg的H.264解码器调用decode_slice()函数完成了解码工作.这些解码工作可以大体上分为3个步骤:熵解码,宏块解码以及环路滤波.本文分析这3个步骤中的第1个步骤. 函数调用关系图 熵解码(Entropy Decoding)部分的源代码在整个H.264解码器中的位置如下图所示. 单击查看更清晰的图片 熵解码(Entropy Decoding)部分的源代码的调用关系如下图所示. 单击查

H.264解码过程剖析-4

x264开源工程实现H.264的视频编码,但没有提供对应的解码器.ffmpeg开源多媒体编解码集合汇集了市面上几乎所有媒体格式的编解码的源代码.其中的H264.c就是一个能正常解码x264编码码流的独立的源文件,其使用步骤也与上述的编码或解码CODEC应用案例基本相同.这一节通过自顶向下的方式,讲述H264.c如何实现H.264视频解码过程. H264.c源文件有几千行,代码量庞大,很不便于浏览.分析和移植.同时该文件还依赖其他源文件,组织结构较复杂,实现平台由于不是基于Windows的VC++

Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞

Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description In a Famous TV Show “Find Out” there are n characters and only one Horrible Truth. To make the series breathtaking all way long, the sc

关于禁止ViewPager预加载问题【转】

转自:http://blog.csdn.net/qq_21898059/article/details/51453938#comments 我最近上班又遇到一个小难题了,就是如题所述:ViewPager预加载的问题.相信用过ViewPager的人大抵都有遇到过这种情况,网上的解决办法也就那么几个,终于在我自己不断试验之下,完美解决了(禁止了)ViewPager的预加载. 好了,首先来说明一下,什么是ViewPager的预加载:ViewPager有一个 "预加载"的机制,默认会把View