https://school.programmers.co.kr/learn/courses/30/lessons/42747
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
문제
n편의 논문 중, h번 이상 인용된 논문이 h편 이상이고 나머지 논문이 h번 이하 인용되었다면 h의 최댓값인 H-index를 찾기
초기 코드(통과 X)
합계: 6.3 / 100.0
문제점 : 논문인용횟수와 habove만을 비교해서, [10,50,100] 배열은 통과가 안됨 예시) 3 >= 10 && 10 >= 0
public int Solution(int[] citations){
int answer = 0;
ArrayList<Integer> hindexs = new ArrayList<>();
for(int i = 0; i < citations.length; i++){
int h = citations[i];
int habove = 0;
int hbelow = 0;
for(int j =0; j < citations.length; j++){
if( h <= citations[j]){ //3 >= 10 && 10 >= 0 발생
habove++;
}else {
hbelow++;
}
}
if ( (habove >= h) && (h > hbelow)){
hindexs.add(h);
}
}
Collections.sort(hindexs, Collections.reverseOrder());
answer = hindexs.get(0);
return answer;
}
개선 코드 (통과 O)
풀이 방법 (ArrayList 배열을 처음에 오름차순 하지 않고 풀기)
{ 3,0,6,1, 5 } 배열이 있을때, citations.length(배열의 수)만큼 'i vs citations[i] 이상인지 비교하고 Arryalist에 넣는다.
i=0의 경우 habove = 5, i=1의 경우 habove = 4, i=2의 경우 habov = 3, i=3의 경우 haboe = 3
i=4의 경우 habove = 2, i=5의 경우 habove = 1이 되어 정답은 3.
public int Solution(int[] citations){
int answer = 0;
ArrayList<Integer> hindexs = new ArrayList<>();
for(int i = 0; i <= citations.length; i++){
int habove = 0;
int hbelow = 0;
for(int j =0; j < citations.length; j++){
if( i <= citations[j] ){
habove++;
}else {
hbelow++;
}
}
if ( (habove >= i) && (i >= hbelow) ){
hindexs.add(i);
}
}
Collections.sort(hindexs, Collections.reverseOrder());
answer = hindexs.get(0);
return answer;
}
}
| 인텔리제이 새창 띄우기 (한글) (0) | 2026.05.17 |
|---|---|
| MySQL Workbench 폰트, 폰트 크기 변경방법 (0) | 2026.05.17 |
| [Java]프로그래머스 - 디스크 컨트롤러 (0) | 2026.05.13 |
| VS Code 자동정렬 Configure Default Formatter 팝업 (1) | 2026.04.27 |
| 구글 앱스크립트 doPost()시 HTML 반환 에러 (0) | 2023.08.19 |
댓글 영역