-
Notifications
You must be signed in to change notification settings - Fork 0
/
275.c
41 lines (37 loc) · 922 Bytes
/
275.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
/*
int hIndex(int* citations, int citationsSize){
for(int i = 0; i < citationsSize; ++i) if(citations[i] >= citationsSize - i) return citationsSize - 1;
return 0;
}
*/
//위의 코드는 O(n)의 시간복잡도
//Binary Seacrh를 쓰면 O(log n)으로 해결 가능
int binary_search(int arr[], int l, int r, int n)
{
int ans = 0;
while(l <= r)
{
int mid = l + (r - l) / 2;
if(arr[mid] >= n - mid)
{
ans = n - mid;
r = mid - 1;
}
else l = mid + 1;
}
return ans;
}
int hIndex(int* citations, int citationsSize)
{
return binary_search(citations, 0, citationsSize - 1, citationsSize);
}
/*
Best Solution
int hIndex(int* citations, int citationsSize){
for (int i = citationsSize ; i > 0 ; i--){
if (citations[i-1] < citationsSize-i+1) return citationsSize-i;
}
return citationsSize;
}
*/