TIL (Today I Learned)

99클럽 코테 스터디 12일차 TIL + 오늘의 학습 키워드

남 희 2024. 8. 3. 08:41

☑️ 문제: H-Index

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

 

프로그래머스

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

programmers.co.kr

 

☑️ 처음 시도한 Code

완전 탐색으로 풀었다.
class Solution {
    public int solution(int[] citations) {
        int n = citations.length;

        int answer = 0;
        for (int h = 10000; h >= 0; h--) {
            int count = 0;
            for (int i = 0; i < n; i++) {
                if (citations[i] >= h) { 
                    count++; 
                }
            }
            
            if (count >= h) {
                answer = h;
                break;
            }
        }
        
        return answer;
    }
}
더보기

Kotllin 코드

class Solution {
    fun solution(citations: IntArray): Int {
        val n = citations.size
        
        var answer = 0
        for (h in 10000 downTo 0) {
            var count = 0
            for (item in citations) {
                if (item >= h) {
                    count++
                }
            }
            if (count >= h) {
                answer = h
                break
            }
        }
        
        return answer
    }
}

 

하지만 고민해보니, 이런 카테고리에 분류된 이유를 깨달았다.

코드를 개선해서 속도를 개선해 보겠다.