import java.util.Arrays; /** * https://leetcode.com/articles/h-index/ */ /** * TestCase * [0] * [1] * [] * [100] */ public class HIndex { // èæ¶4msï¼æ¶é´å¤æåº¦O(nlgn) public int hIndex(int[] citations) { Arrays.sort(citations); int hIndex = 0; for (int i = citations.length - 1; i >= 0; i--) { hIndex = Math.max(hIndex, Math.min(citations.length - i, citations[i])); } return hIndex; } // èæ¶1msï¼æ¶é´å¤æåº¦O(n) public int hIndex2(int[] citations) { /** * å¤§äºæç« æ°çå¼ç¨å¯ä»¥åå¹¶å°ä¸èµ· */ int n = citations.length; int[] f = new int[n + 1]; for (int k : citations) { f[Math.min(k, n)]++; } int hindex = 0; /** * i表示å¼ç¨æ°ï¼j表示大äºçäºè¯¥å¼ç¨æ°çæ»æç« æ° */ for (int i = n, j = 0; i >= 0; i--) { j += f[i]; hindex = Math.max(hindex, Math.min(j, i)); } return hindex; } }