흠... 지금 코딜리티 플랫폼이 먹통이라 정답체크는 못 해봤다.
skt 코딩테스트때문에 먹통됐다던데 ㅠ
아무튼 문제는 그리 어렵지 않았다. 약간 indexedTree 의 문제에 착안해서 풀었다.
꼭 클래스를 안써서 할수도 있었겠지만, 딕셔너리 값을 갖기위해 클래스를 사용해서 편하게 풀었다.
여기서 주의해야할 점은, 정렬할때 포지션이 동률일 경우에는 무조건 시간이 작은순으로 정렬해야한다는점, 그리고 포지션이 동률일 경우 그 다음에 나오는 시간을 무시해야한다는 점이다.
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.Arrays;
class Solution {
public static class node implements Comparable<node>{
int position;
int time;
public node(int time, int position) {
this.time = time;
this.position = position;
}
@Override
public int compareTo(node o) {
int pos = Integer.compare(this.position, o.position);
if(pos == 0) {
return Integer.compare(this.time, o.time);
}
return pos;
}
}
public int solution(int X, int[] A) {
// write your code in Java SE 8
node[] list = new node[A.length];
for(int i = 0; i < A.length; i++) {
list[i] = new node(i,A[i]);
}
Arrays.sort(list);
int max = 0;
boolean flag = false;
int curr = 0;
for(node em : list) {
if(em.position == curr) {
continue;
}
if(curr + 1 != em.position){
break;
}
curr = em.position;
if(em.time > max) {
max = em.time;
}
if( em.position == X) {
flag = true;
break;
}
}
if (!flag){
max = -1;
}
return max;
}
}