https://school.programmers.co.kr/learn/courses/30/lessons/250121#
import java.util.*;
class Solution {
public int[][] solution(int[][] data, String ext, int val_ext, String sort_by) {
int indexExt = -1;
int indexSort = -1;
if (ext.equals("code")){
indexExt = 0;
} else if (ext.equals("date")) {
indexExt = 1;
} else if (ext.equals("maximum")) {
indexExt = 2;
} else if (ext.equals("remain")) {
indexExt = 3;
}
if (sort_by.equals("code")){
indexSort = 0;
} else if (sort_by.equals("date")) {
indexSort = 1;
} else if (sort_by.equals("maximum")) {
indexSort = 2;
} else if (sort_by.equals("remain")) {
indexSort = 3;
}
List<Data> list = new ArrayList<>();
for(int[] d: data) {
if (d[indexExt] >= val_ext) {
continue;
}
list.add(new Data(d, indexSort));
}
Collections.sort(list);
int[][] answer = new int[list.size()][4];
int index = 0;
for(Data d: list) {
answer[index] = d.getArray();
index += 1;
}
return answer;
}
}
class Data implements Comparable<Data>
{
public int code;
public int date;
public int maximum;
public int remain;
public int standard;
public Data(int[] data, int standard) {
this.code = data[0];
this.date = data[1];
this.maximum = data[2];
this.remain = data[3];
this.standard = standard;
}
@Override
public int compareTo(Data o) {
switch(standard) {
case 0:
return compareCode(o);
case 1:
return compareDate(o);
case 2:
return compareMaximum(o);
default:
return compareRemain(o);
}
}
public int compareCode(Data o)
{
return this.code - o.code;
}
public int compareDate(Data o)
{
return this.date - o.date;
}
public int compareMaximum(Data o)
{
return this.maximum - o.maximum;
}
public int compareRemain(Data o)
{
return this.remain - o.remain;
}
public int[] getArray() {
return new int[] {this.code, this.date, this.maximum, this.remain};
}
}
'코딩테스트' 카테고리의 다른 글
[programmers][PCCE] 9. 이웃한 칸 (0) | 2024.03.24 |
---|---|
[programmers][PCCP] 2번 / 석유 시추 (0) | 2024.03.21 |
[programmers] 이모티콘 할인행사 (0) | 2024.03.13 |
33. Search in Rotated Sorted Array (0) | 2024.02.22 |
994. Rotting Oranges (0) | 2024.02.21 |