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

728x90

 

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

1. Java

import java.util.*;
class Solution {
    public int[] solution(int k, int[] score) {
        int[] answer = new int[score.length];
        List<Integer> list = new ArrayList<>();
        for ( int i = 0; i < score.length; ++i ) {
            list.add(score[i]);
            Collections.sort(list);
            if ( list.size() > k ) {
                list.remove(0);
            }
            answer[i] = list.get(0);
        }
        return answer;
    }
}

 

2. Python

def solution(k, score):
    answer = []
    list = []
    for i in range(len(score)) :
        list.append(score[i])
        list.sort()
        if len(list) > k :
            list.pop(0)
        answer.append(list[0])
    return answer

 

반응형