Skip to content

Commit b78714c

Browse files
committed
정렬
1 parent 5d02dfb commit b78714c

1 file changed

Lines changed: 38 additions & 14 deletions

File tree

PROGRAMMERS/정렬/H-index.js

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,45 @@
11
function solution(citations) {
22
let answer = 0;
33
const n = citations.length;
4-
const tmp = citations.sort((a,b) => a-b);
5-
if(tmp[0] >= n) return n;
4+
const tmp = citations.sort((a, b) => a - b);
5+
if (tmp[0] >= n) return n;
66
tmp.map((h, i) => {
7-
if(n-i >= h){
8-
answer = h;
9-
if(i+1 < n){
10-
const next = tmp[i+1]-h-1;
11-
for(let j=h+1; j<=h+next; j++){
12-
if(n-i-1 >= j){
13-
answer = j;
14-
}
15-
}
7+
if (n - i >= h) {
8+
answer = h;
9+
if (i + 1 < n) {
10+
const next = tmp[i + 1] - h - 1;
11+
for (let j = h + 1; j <= h + next; j++) {
12+
if (n - i - 1 >= j) {
13+
answer = j;
1614
}
15+
}
1716
}
18-
})
19-
17+
}
18+
});
19+
2020
return answer;
21-
}
21+
}
22+
23+
function solution2(citations) {
24+
const clen = citations.length;
25+
const list = citations.sort((a, b) => a - b);
26+
27+
if (list[clen - 1] === 0) return 0;
28+
let prev = 0,
29+
next = clen;
30+
31+
list.forEach((c, i) => {
32+
const r = clen - i;
33+
if (c >= i + 1 && r >= c) {
34+
prev = c;
35+
next = r - 1;
36+
}
37+
});
38+
39+
let result = prev;
40+
for (let i = prev + 1; i <= next; i++) {
41+
if (i > next) return result;
42+
else result = i;
43+
}
44+
return result;
45+
}

0 commit comments

Comments
 (0)