Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7376ddd

Browse files
solves h index ii
1 parent ccdfef5 commit 7376ddd

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@
224224
| 270 | 🔒 [Closest Binary Search Tree Value](https://leetcode.com/problems/closest-binary-search-tree-value) | | |
225225
| 271 | 🔒 [Encode and Decode Strings](https://leetcode.com/problems/encode-and-decode-strings) | | |
226226
| 274 | [H-Index](https://leetcode.com/problems/h-index) | [![Java](assets/java.png)](src/HIndex.java) | |
227-
| 275 | [H-Index II](https://leetcode.com/problems/h-index-ii) | | |
227+
| 275 | [H-Index II](https://leetcode.com/problems/h-index-ii) | [![Java](assets/java.png)](src/HIndexII.java) | |
228228
| 276 | 🔒 [Paint Fence](https://leetcode.com/problems/paint-fence) | | |
229229
| 277 | 🔒 [Find The Celebrity](https://leetcode.com/problems/find-the-celebrity) | | |
230230
| 278 | [First Bad Version](https://leetcode.com/problems/first-bad-version) | [![Java](assets/java.png)](src/FirstBadVersion.java) [![Python](assets/python.png)](python/first_bad_version.py) | |

‎src/HIndexII.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// https://leetcode.com/problems/h-index-ii
2+
// T: O(log(N))
3+
// S: O(1)
4+
5+
public class HIndexII {
6+
public static void main(String[] args) {
7+
System.out.println(hIndex(new int[] {0, 5, 5, 5, 5}));
8+
}
9+
10+
public static int hIndex(int[] citations) {
11+
int left = 0, right = citations.length - 1, middle;
12+
while (left <= right) {
13+
middle = left + (right - left) / 2;
14+
if (citations[middle] == citations.length - middle) return citations[middle];
15+
else if (citations[middle] > citations.length - middle) right = middle - 1;
16+
else left = middle + 1;
17+
}
18+
return citations.length - left;
19+
}
20+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /