https://school.programmers.co.kr/learn/courses/30/lessons/138477?language=java
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
이 문제는 다양한 방법으로 풀 수 있을 것 같다. 풀고나서 다시 생각해보니 PriorityQueue를 사용해서 애초에 reverseOrder로 comparator를 쓰면 중간중간에 Collections.sort를 반복문 안에 처리 안해줘도 될 것 같다. 어쨌든 기본적인 자료구졸를 알면 해결할 수 있는 문제였다.
문제 및 입출력
코드
import java.util.ArrayList;
import java.util.Collections;
class Solution
{
public Object[] solution(int k, int[] score)
{
ArrayList<Integer> temp = new ArrayList<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i=0; i<score.length; i++)
{
temp.add(score[i]);
Collections.sort(temp);
if(temp.size()>k)
{
list.add(temp.get(temp.size()-k));
}
else
{
list.add(temp.get(0));
}
}
return list.toArray();
}
}
'[ 다먹살 ] > - Coding' 카테고리의 다른 글
[프로그래머스] 레벨2 n² 배열 자르기 (0) | 2023.02.08 |
---|---|
[프로그래머스] 레벨3 대여 횟수가 많은 자동차들의 월별 대여 횟수 구하기 (0) | 2023.02.08 |
[프로그래머스] 레벨1 크기가 작은 부분 문자열 (0) | 2023.02.06 |
[프로그래머스] 레벨1 개인정보 수집 유효기간 (0) | 2023.02.03 |
[프로그래머스] 레벨1 가장 가까운 같은 글자 (0) | 2022.12.16 |
댓글