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

資訊專欄INFORMATION COLUMN

H-Index & II

mochixuan / 3091人閱讀

H-Index

題目鏈接:https://leetcode.com/problems...

sort:

public class Solution {
    public int hIndex(int[] citations) {
        if(citations == null || citations.length == 0)  return 0;
        // sort
        Arrays.sort(citations);
        int n = citations.length;
        // find 1st i that citations[i] < i
        // [0, 1, 3, 5, 6]
        for(int i = 0; i < n; i++) {
            if(citations[i] >= n - i) return n - i;
        }
        return 0;
    }
}

calculate suffix sum:

public class Solution {
    public int hIndex(int[] citations) {
        if(citations == null || citations.length == 0)  return 0;
        int n = citations.length;
        int[] count = new int[n + 1];
        for(int i = 0; i < n; i++) {
            if(citations[i] > n) {
                count[n]++;
            }
            else {
                count[citations[i]]++;
            }
        }
        
        // calculate suffix sum
        int sum = 0;
        for(int i = n; i >= 0; i--) {
            sum += count[i];
            if(sum >= i) return i;
        }
        return 0;
    }
}
H-Index II

題目鏈接:https://leetcode.com/problems...

public class Solution {
    public int hIndex(int[] citations) {
        if(citations == null || citations.length == 0)  return 0;
        // binary search, find 1st c[i] >= n - i
        int n = citations.length;
        int l = 0, r = n - 1;
        while(l + 1 < r) {
            int mid = l + (r - l) / 2;
            int val = citations[mid];
            if(val == n - mid) return n - mid;
            else if(val < n - mid) l = mid;
            else r = mid;
        }
        
        if(citations[l] >= n - l) return n - l;
        if(citations[r] >= n - r) return n - r;
        return 0;
    }
}

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

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

相關(guān)文章

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

    摘要:排序法復(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 research...

    xumenger 評(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
  • 490. The Maze &amp;&amp; 505. The Maze II

    摘要:題目鏈接和上一題不一樣的是這道題要求最短的路徑,普通的遍歷和都是可以做的,但是求最短路徑的話還是用。這里相當(dāng)于每個(gè)點(diǎn)有至多條相連,每條的就是到墻之前的長(zhǎng)度。 490. The Maze 題目鏈接:https://leetcode.com/problems... 又是圖的遍歷問題,就是簡(jiǎn)單的遍歷,所以dfs和bfs都可以做,復(fù)雜度也是一樣的。這道題要求球不能停下來,即使碰到destina...

    BoYang 評(píng)論0 收藏0
  • [LeetCode] Path Sum (I &amp; II &amp; III)

    摘要: 112. Path Sum Problem Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Note: A leaf is a node...

    張金寶 評(píng)論0 收藏0
  • [LintCode/LeetCode] Single Number I &amp; II [位運(yùn)算]

    摘要:整個(gè)過程相當(dāng)于,直接在和里去掉既是又是的。所以最后返回的,一定是只出現(xiàn)過一次的,而出現(xiàn)兩次的都在里,出現(xiàn)三次的都被消去了。 Single Number I Problem Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Example Given [1,2,2,1,3,4,3], return...

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

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

0條評(píng)論

mochixuan

|高級(jí)講師

TA的文章

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