JAVAIARY

프로그래머스) H-Index 본문

examplePractice

프로그래머스) H-Index

shiherlis 2023. 3. 4. 14:59

문제: https://school.programmers.co.kr/learn/courses/30/lessons/42747

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

import java.util.Arrays;

class Solution {
    public int solution(int[] citations) {
        int answer = 0;
        Arrays.sort(citations);
		// 논문 수가 1일때
		if (citations.length == 1) {
			answer = citations[0] == 0 ? answer : answer+1 ;
			return answer;
		}
		// 인용 횟수가 h 이상인 논문이 h개 이상이어야 함
		while(citations.length >answer
				&& citations[(citations.length-1)-answer] > answer ) {
			answer++;
		}
        return answer;
    }
}
  • 테스트 케이스 9번에서 자꾸 런타임 오류가 났었는데 , 인덱스 문제인 걸 알아도 이해가 잘 안됐다.
  • 반례 {10,20,30} 을 돌려보면서 찾음
  • while문 내의 조건식에도 순서가 중요함.
  • 논리곱으로 묶었지만 실행순서는 1, 2 로 정해져있으므로 런타임에러가 발생하지 않도록 순서의 조정이 필요

점수 많이 받아서 기분 좋았음 ㅎㅎ