코딩테스트
[프로그래머스]PCCE 기출문제[PCCE 기출문제] 10번 / 데이터 분석 자바
mhui123
2025. 3. 28. 11:53
반응형
https://school.programmers.co.kr/learn/courses/30/lessons/250121
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
주어진 data에서 ext 값을 기준으로 ext < val_ext 인 항목만 추출하여 sort_by를 기준으로 오름차순으로 정렬하여 반환하는 문제.
ext와 sort_by값을 data의 인덱스와 연동하기 위해 Map을 활용하였고,
추출한 값은 List<int[]>에 담은 후
//sortCol 기준 오름차순 정렬
filtered.sort(Comparator.comparingInt(a -> a[sortCol]));
//내림차순
filtered.sort(Comparator.comparingInt((int[] a) -> a[sortCol]).reversed());
를 활용해 오름차순 정렬 한 뒤 int[][]에 담아 반환하여 처리하였다.
import java.util.*;
class Solution {
public int[][] solution(int[][] data, String ext, int val_ext, String sort_by) {
Map<String, Integer> typeMap = new HashMap<>();
typeMap.put("code", 0);
typeMap.put("date", 1);
typeMap.put("maximum", 2);
typeMap.put("remain", 3);
int col = typeMap.get(ext);
int sortCol = typeMap.get(sort_by);
List<int[]> filtered = new ArrayList<>();
for (int[] datum : data) {
if (datum[col] < val_ext) {
filtered.add(datum);
}
}
filtered.sort(Comparator.comparingInt(a -> a[sortCol]));
int[][] answer = new int[filtered.size()][filtered.get(0).length];
for(int i = 0; i < filtered.size(); i ++){
answer[i] = filtered.get(i);
}
return answer;
}
}
반응형