1 public class Solution {
 2     public int hIndex(int[] citations) {
 3         if (citations.length == 0) {
 4             return 0;
 5         }
 6         int[] cited = new int[citations.length + 1];
 7         for (int c : citations) {
 8             cited[c > citations.length ? citations.length : c]++;
 9         }
10         
11         int result = 0;
12         for (int i = citations.length; i >= 0; i--) {
13             result += cited[i];
14             if (result >= i) {
15                 return i;
16             }
17         }
18         return 0;
19     }
20 }

O(n) space, O(n) time.

 

 1 public class Solution {
 2     public int hIndex(int[] citations) {
 3         Arrays.sort(citations);
 4         int i = 0;
 5         while (i < citations.length && citations[citations.length - i - 1] > i) {
 6             i++;
 7         }
 8         return i;
 9     }
10 }

it costs more for java sort primitive type in descending order. So use reversed index to get the idea.

posted on 2016-07-07 13:49  keepshuatishuati  阅读(152)  评论(0编辑  收藏  举报