摘要:題目解答滿足這個(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 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.
Hint:
An easy approach is to sort the array first.
What are the possible values of h-index?
A faster approach is to use extra space.
解答:
滿足這個(gè)index的最大值不會(huì)超過citations數(shù)組的len, 因?yàn)槿绻^了,就不可能有這么多的paper數(shù)。所以brute force就是把所有可能的n個(gè)paper至少有n個(gè)citation的n記下來,然后找出最大的n。
代碼:
public int hIndex(int[] citations) { int maxHIndex = 0; for (int i = citations.length; i >= 0; i--) { int citNum = i; int count = 0; for (int j = 0; j < citations.length; j++) { if (citations[j] >= citNum) { count++; } } if (count >= citNum) { maxHIndex = Math.max(maxHIndex, citNum); } } return maxHIndex; }
Follow up是犧牲空間來換取時(shí)間,那就用另外一個(gè)數(shù)組來存當(dāng)前index存在的文章數(shù),然后從后往前相加,如果滿足條件就輸出這個(gè)最大的index。
舉個(gè)例子:
citations: 3, 0, 6, 1, 5 arr: 0 1 2 3 4 5 val: 1 1 1 2
那么從后向前掃的時(shí)候,記一個(gè)count,掃到arr(5)的時(shí)候,count=2 < 5, 不滿足;掃arr(4),count=2 < 4, 不滿足;掃arr(3), count=2+1=3 == 3, 滿足。因?yàn)槭菑暮笙蚯皰叩?,所以?dāng)前的index就是滿足條件的最大數(shù)。
代碼:
public int hIndex(int[] citations) { if (citations == null || citations.length == 0) return 0; int len = citations.length; int[] arr = new int[len + 1]; for (int i = len - 1; i >= 0; i--) { //最大的index也不會(huì)大于數(shù)組的長度,所以,超過數(shù)組長度的citation可以都記在len里 if (citations[i] > len) { arr[len] += 1; } else { //arr的index就是一篇文章中citation的個(gè)數(shù),arr的值就是有這么多citation的文章的個(gè)數(shù) arr[citations[i]] += 1; } } int count = 0; for (int i = len; i >= 0; i--) { count += arr[i]; if (count >= i) { return i; } } return 0; }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/64838.html
摘要:排序法復(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...
H-Index 題目鏈接:https://leetcode.com/problems... sort: public class Solution { public int hIndex(int[] citations) { if(citations == null || citations.length == 0) return 0; // sort ...
摘要:數(shù)據(jù)問題解壓縮結(jié)果計(jì)算高主要運(yùn)行結(jié)果列表容差計(jì)算通過的內(nèi)容算法點(diǎn)列表第一個(gè)點(diǎn)最后一個(gè)點(diǎn)容差軌跡結(jié)果原始圖壓縮圖 數(shù)據(jù) P0,107.605,137.329 P1,122.274,169.126 P2,132.559,179.311 P3,153.324,184.276 P4,171.884,174.654 P5,186.408,168.634 P6,196.566,145.204 P7...
摘要:重點(diǎn)以上版本參數(shù)化都需要借助進(jìn)行參數(shù)化,需嚴(yán)格縮進(jìn)格式,不能用控制縮進(jìn),只能用空格控制直接引用列表進(jìn)行參數(shù)化引用文件進(jìn)行參數(shù)化借助輔助函數(shù)進(jìn)行參數(shù)化定義項(xiàng)目的文件框架建立四個(gè)文件夾,分別用來存放接口用例用例集測試數(shù)據(jù)編寫接口腳本在文件下, 重點(diǎn):2.x以上版本參數(shù)化都需要借助testsuit...
摘要:安裝執(zhí)行版本號(hào),例如以下語句可以安裝幾的版本好像在墻內(nèi)只能找到以前的版本使用可以查看現(xiàn)有的版本,可以支持模糊切換。 一直說要好好學(xué)習(xí),總結(jié)知識(shí)什么的。一直覺得沒有時(shí)間。周一終于提交了論文盲審。決定從今天每周都總結(jié)一次自己的所學(xué)。希望自己能堅(jiān)持。 任務(wù)描述: 一個(gè)醫(yī)學(xué)系的同學(xué)要分析一個(gè)叫TCGA的數(shù)據(jù)庫,每個(gè)實(shí)驗(yàn)文件是txt,格式如下: hsa-miR-1228* 5.185500...
閱讀 819·2023-04-26 01:30
閱讀 3371·2021-11-24 10:32
閱讀 2273·2021-11-22 14:56
閱讀 2095·2021-11-18 10:07
閱讀 616·2019-08-29 17:14
閱讀 698·2019-08-26 12:21
閱讀 3167·2019-08-26 10:55
閱讀 3020·2019-08-23 18:09