[프로그래머스/정렬] Level 2 H-Index (JAVA)

Jiwoo Kim·2021년 3월 18일
0

알고리즘 정복하기

목록 보기
31/85
post-thumbnail

문제


풀이

문제 설명이 참 불친절했던 것 같다. 나만 난독인줄 알았는데 그건 아닌 것 같아서 다행!

  1. countLargerThan(): 인용횟수가 주어진 target 이상인 원소의 개수를 센다.
  2. hcountLargerThan(h) 중 작은 값을 구한다. 이것이 현재 citation에 대한 h 값이다.
  3. 모든 citation을 탐색하며 h 값 중 가장 큰 값을 구해서 리턴한다.

어차피 O(m*n)의 복잡도로 처음부터 끝까지 for문 탐색이 진행되는데 왜 카테고리가 정렬인지 모르겠다. 정렬이 필수적이지는 않은 것 같다.


코드

import java.util.*;

class Solution {

    public int solution(int[] citations) {
        int answer = 0;
        for (int citation : citations)
            answer = Math.max(Math.min(countLargerThan(citation, citations), citation), answer);
        return answer;
    }

    private int countLargerThan(int target, int[] citations) {
        int count = 0;
        for (int citation : citations)
            if (citation >= target) count++;
        return count;
    }
} 

0개의 댓글