亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[Leetcode] H-Index H指數(shù)

xumenger / 1099人閱讀

摘要:排序法復(fù)雜度時(shí)間空間思路先將數(shù)組排序,我們就可以知道對(duì)于某個(gè)引用數(shù),有多少文獻(xiàn)的引用數(shù)大于這個(gè)數(shù)。代碼排序得到當(dāng)前的指數(shù)數(shù)組映射法復(fù)雜度時(shí)間空間思路也可以不對(duì)數(shù)組排序,我們額外使用一個(gè)大小為的數(shù)組。

H-Index I

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.

排序法 復(fù)雜度

時(shí)間 O(NlogN) 空間 O(1)

思路

先將數(shù)組排序,我們就可以知道對(duì)于某個(gè)引用數(shù),有多少文獻(xiàn)的引用數(shù)大于這個(gè)數(shù)。對(duì)于引用數(shù)citations[i],大于該引用數(shù)文獻(xiàn)的數(shù)量是citations.length - i,而當(dāng)前的H-Index則是Math.min(citations[i], citations.length - i),我們將這個(gè)當(dāng)前的H指數(shù)和全局最大的H指數(shù)來比較,得到最大H指數(shù)。

代碼
public class Solution {
    public int hIndex(int[] citations) {
        // 排序
        Arrays.sort(citations);
        int h = 0;
        for(int i = 0; i < citations.length; i++){
            // 得到當(dāng)前的H指數(shù)
            int currH = Math.min(citations[i], citations.length - i);
            if(currH > h){
                h = currH;
            }
        }
        return h;
    }
}
數(shù)組映射法 復(fù)雜度

時(shí)間 O(N) 空間 O(N)

思路

也可以不對(duì)數(shù)組排序,我們額外使用一個(gè)大小為N+1的數(shù)組stats。stats[i]表示有多少文章被引用了i次,這里如果一篇文章引用大于N次,我們就將其當(dāng)為N次,因?yàn)镠指數(shù)不會(huì)超過文章的總數(shù)。為了構(gòu)建這個(gè)數(shù)組,我們需要先將整個(gè)文獻(xiàn)引用數(shù)組遍歷一遍,對(duì)相應(yīng)的格子加一。統(tǒng)計(jì)完后,我們從N向1開始遍歷這個(gè)統(tǒng)計(jì)數(shù)組。如果遍歷到某一個(gè)引用次數(shù)時(shí),大于或等于該引用次數(shù)的文章數(shù)量,大于引用次數(shù)本身時(shí),我們可以認(rèn)為這是H指數(shù)。之所以不用再向下找,因?yàn)槲覀円∽畲蟮腍指數(shù)。那如何求大于或等于某個(gè)引用次數(shù)的文章數(shù)量呢?我們可以用一個(gè)變量,從高引用次的文章數(shù)累加下來。因?yàn)槲覀冎?,如果有x篇文章的引用大于等于3次,那引用大于等于2次的文章數(shù)量一定是x加上引用次數(shù)等于2次的文章數(shù)量。

代碼
public class Solution {
    public int hIndex(int[] citations) {
        int[] stats = new int[citations.length + 1];
        int n = citations.length;
        // 統(tǒng)計(jì)各個(gè)引用次數(shù)對(duì)應(yīng)多少篇文章
        for(int i = 0; i < n; i++){
            stats[citations[i] <= n ? citations[i] : n] += 1;
        }
        int sum = 0;
        // 找出最大的H指數(shù)
        for(int i = n; i > 0; i--){
            // 引用大于等于i次的文章數(shù)量,等于引用大于等于i+1次的文章數(shù)量,加上引用等于i次的文章數(shù)量 
            sum += stats[i];
            // 如果引用大于等于i次的文章數(shù)量,大于引用次數(shù)i,說明是H指數(shù)
            if(sum >= i){
                return i;
            }
        }
        return 0;
    }
}
H-Index II

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

二分搜索法 復(fù)雜度

時(shí)間 O(logN) 空間 O(1)

思路

在升序的引用數(shù)數(shù)組中,假設(shè)數(shù)組長為N,下標(biāo)為i,則N - i就是引用次數(shù)大于等于下標(biāo)為i的文獻(xiàn)所對(duì)應(yīng)的引用次數(shù)的文章數(shù)。如果該位置的引用數(shù)小于文章數(shù),則說明則是有效的H指數(shù),如果一個(gè)數(shù)是H指數(shù),那最大的H指數(shù)一定在它的后面(因?yàn)槭巧虻模?。根?jù)這點(diǎn)就可已進(jìn)行二分搜索了。這里min = mid + 1的條件是citations[mid] < n - mid,確保退出循環(huán)時(shí)min肯定是指向一個(gè)有效的H指數(shù)。

代碼
public class Solution {
    public int hIndex(int[] citations) {
        int n = citations.length;
        if(n == 0) return 0;
        int min = 0, max = citations.length - 1;
        while(min <= max){
            int mid = (min + max) / 2;
            // 如果該點(diǎn)是有效的H指數(shù),則最大H指數(shù)一定在右邊
            if(citations[mid] < n - mid){
                min = mid + 1;
            // 否則最大H指數(shù)在左邊
            } else {
                max = mid - 1;
            }
        }
        // n - min是min點(diǎn)的H指數(shù)
        return n - min;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/64578.html

相關(guān)文章

  • H-Index & II

    H-Index 題目鏈接:https://leetcode.com/problems... sort: public class Solution { public int hIndex(int[] citations) { if(citations == null || citations.length == 0) return 0; // sort ...

    mochixuan 評(píng)論0 收藏0
  • 274. H-Index

    摘要:題目解答滿足這個(gè)的最大值不會(huì)超過數(shù)組的因?yàn)槿绻^了,就不可能有這么多的數(shù)。所以就是把所有可能的個(gè)至少有個(gè)的記下來,然后找出最大的。因?yàn)槭菑暮笙蚯皰叩模援?dāng)前的就是滿足條件的最大數(shù)。 題目:Given an array of citations (each citation is a non-negative integer) of a researcher, write a fun...

    xiaochao 評(píng)論0 收藏0
  • leetcode50 Pow(x, n)自定義實(shí)現(xiàn)指數(shù)運(yùn)算

    摘要:題目要求此處為題目鏈接即用自己的代碼實(shí)現(xiàn)指數(shù)運(yùn)算。指數(shù)為負(fù)數(shù)即求其倒數(shù)。思路一二分法計(jì)算這題的思路我之前的一篇博客思路基本相同。所以在能轉(zhuǎn)換為循環(huán)的情況下還是最好使用循環(huán)來解決。 題目要求 此處為題目鏈接即用自己的代碼實(shí)現(xiàn)指數(shù)運(yùn)算。指數(shù)運(yùn)算一般有兩種情況,即指數(shù)為整數(shù)和指數(shù)為負(fù)數(shù)的情況。指數(shù)為負(fù)數(shù)即求其倒數(shù)。 思路一:二分法計(jì)算 這題的思路我之前的一篇博客思路基本相同。有興趣的可以直接...

    DoINsiSt 評(píng)論0 收藏0
  • 70道前端LeetCode題目集合及視頻講解(持續(xù)更新中...)

    前端LeetCode刷題 下面是已刷的題目的目錄。GitHub:https://github.com/cunzaizhuy...每日打卡更新中,歡迎關(guān)注。 數(shù)組類 26 刪除排序數(shù)組中的重復(fù)項(xiàng) 27 移除元素 35 搜索插入位置 66 加1 80 medium 刪除排序數(shù)組中的重復(fù)項(xiàng)2 88 合并兩個(gè)有序數(shù)組 167 兩數(shù)之和II - 輸入有序數(shù)組 118 楊輝三角 169 easy 求眾數(shù) 1...

    mayaohua 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<