Algorithms/프로그래머스

[프로그래머스][Java] 명예의 전당 (1)

hyunki.Dev 2023. 2. 4. 18:21

문제

[명예의 전당 (1)]

 

프로그래머스

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

programmers.co.kr

 

풀이

  • ❗️알고리즘을 굳이 생각하지 않아도 문제의 규칙대로 구현하면 되는 구현 문제
  • ❗️명예의 전당에 등록할 수 있는 수만큼 점수를 등록하면서 명예의 전당 리스트 사이즈가 == k  일때 
  • 가장 작은 최소 점수와 비교대상인 score[i] 점수보다 크냐 작냐에 따라 다른 규칙을 실행하면 됩니다. 

 

코드

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Collections;


class Solution {
    public int[] solution(int k, int[] score) {
        // int[] answer = {};
        
        List<Integer> hall_of_fame_list =  new ArrayList<>();
        List<Integer> minimum_score = new ArrayList<>();
        
        for(int i=0; i < score.length; i++) {
            
            if(hall_of_fame_list.size() == k){
                
                if(hall_of_fame_list.get(0) < score[i]){
                    // System.out.println("i번째 : " + i);
                    hall_of_fame_list.remove(0);
                    
                    // System.out.println("i번째 : " + i + "삭제완료");
                    System.out.println(hall_of_fame_list);
                    // hall_of_fame_list.add(score[i]);
                    // continue;
                }
                else {
                    minimum_score.add(hall_of_fame_list.get(0));
                    continue;
                }
            }
        
            hall_of_fame_list.add(score[i]);
            Collections.sort(hall_of_fame_list);
            
            // System.out.println("i번째 : " + i + " :: " + hall_of_fame_list);
            minimum_score.add(hall_of_fame_list.get(0));
            
        }
        
        return minimum_score
             .stream()
             .mapToInt(Integer::intValue)
             .toArray();
    }
}