Skip to content

Commit 59efd3c

Browse files
authored
Improved task 274.
1 parent 61373bd commit 59efd3c

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3509,7 +3509,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.15'
35093509
| 0279 |[Perfect Squares](src.save/main/java/g0201_0300/s0279_perfect_squares/Solution.java)| Medium | Top_Interview_Questions, Dynamic_Programming, Math, Breadth_First_Search, Dynamic_Programming_I_Day_21 | 1 | 100.00
35103510
| 0278 |[First Bad Version](src.save/main/java/g0201_0300/s0278_first_bad_version/Solution.java)| Easy | Binary_Search, Interactive, Algorithm_I_Day_1_Binary_Search, Binary_Search_I_Day_5, Level_1_Day_7_Binary_Search | 15 | 87.89
35113511
| 0275 |[H-Index II](src.save/main/java/g0201_0300/s0275_h_index_ii/Solution.java)| Medium | Array, Binary_Search, Binary_Search_II_Day_8 | 0 | 100.00
3512-
| 0274 |[H-Index](src.save/main/java/g0201_0300/s0274_h_index/Solution.java)| Medium | Array, Sorting, Counting_Sort | 3 | 45.78
3512+
| 0274 |[H-Index](src.save/main/java/g0201_0300/s0274_h_index/Solution.java)| Medium | Array, Sorting, Counting_Sort | 0 | 100.00
35133513
| 0273 |[Integer to English Words](src.save/main/java/g0201_0300/s0273_integer_to_english_words/Solution.java)| Hard | String, Math, Recursion, Udemy_Strings | 3 | 95.67
35143514
| 0268 |[Missing Number](src.save/main/java/g0201_0300/s0268_missing_number/Solution.java)| Easy | Top_Interview_Questions, Array, Hash_Table, Math, Sorting, Binary_Search, Bit_Manipulation | 1 | 72.07
35153515
| 0264 |[Ugly Number II](src.save/main/java/g0201_0300/s0264_ugly_number_ii/Solution.java)| Medium | Hash_Table, Dynamic_Programming, Math, Heap_Priority_Queue, Dynamic_Programming_I_Day_11 | 2 | 99.91
Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,21 @@
11
package g0201_0300.s0274_h_index;
22

3-
// #Medium #Array #Sorting #Counting_Sort #2022_07_06_Time_3_ms_(45.78%)_Space_41.7_MB_(75.73%)
4-
5-
import java.util.Arrays;
3+
// #Medium #Array #Sorting #Counting_Sort #2022_11_05_Time_0_ms_(100.00%)_Space_40.3_MB_(86.98%)
64

75
public class Solution {
86
public int hIndex(int[] citations) {
9-
// Sort array then traverse from end keep track of counter denoting total elements seen so
10-
// far
11-
Arrays.sort(citations);
12-
int count = 0;
13-
int hIndex = 0;
14-
for (int i = citations.length - 1; i >= 0; i--) {
15-
if (i == citations.length - 1 && count == citations[i]) {
16-
hIndex = citations[i];
17-
return hIndex;
18-
// Ex:- 7 10--> counter =8
19-
} else if (citations[i] <= count && count < citations[i + 1]) {
20-
hIndex = count;
21-
return hIndex;
22-
// Ex:- 7 9 --> counter 6 (incuding 7 there willbe 7 elements)
23-
} else if (citations[i] == count + 1) {
24-
hIndex = count + 1;
25-
return hIndex;
26-
} else {
27-
count++;
28-
}
7+
int len = citations.length;
8+
int[] freqArray = new int[len + 1];
9+
for (int citation : citations) {
10+
freqArray[Math.min(citation, len)]++;
2911
}
30-
// case when no element is hindex so far
31-
if (count < citations[0]) {
32-
hIndex = count;
12+
int total_so_far = 0;
13+
for (int k = len; k >= 0; k--) {
14+
total_so_far += freqArray[k];
15+
if (total_so_far >= k) {
16+
return k;
17+
}
3318
}
34-
return hIndex;
19+
return -1;
3520
}
3621
}

0 commit comments

Comments
 (0)