728x90
https://programmers.co.kr/learn/courses/30/lessons/42587
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Programmers {
public static void main(String[] args) {
System.out.println(solution(new int[]{2, 1, 3, 2}, 2));
System.out.println(solution(new int[]{1, 1, 9, 1, 1, 1}, 0));
System.out.println(solution(new int[]{1, 2, 3, 4, 5, 6, 7, 8}, 0)); // 8
}
static class Pair {
Pair(Integer key, Integer value) {
this.key = key;
this.value = value;
}
Integer key;
Integer value;
Integer getKey() {
return key;
}
Integer getValue() {
return value;
}
}
public static int solution(int[] priorities, int location) {
int loop = 0;
List<Pair> list = new ArrayList<>();
for (int i = 0; i < priorities.length; i++) {
int priority = priorities[i];
list.add(new Pair(i, priority));
}
Iterator<Pair> iterator = list.iterator();
while (iterator.hasNext()) {
for (int i = 0; i < list.size() - 1; i++) {
int cur = list.get(0).getValue();
boolean checked = false;
for (Pair priority : list) {
if (cur < priority.getValue()) {
checked = true;
break;
}
}
if (checked) {
Pair temp = list.get(0);
for (int j = 0; j < list.size() - 1; j++) list.set(j, list.get(j + 1));
list.set(list.size() - 1, temp);
}
}
loop++;
if (list.get(0).getKey() == location) break;
list.remove(0);
}
return loop;
}
}
좀더 고민하자
728x90