2019 KAKAO BLIND RECRUITMENT 무지의 먹방 라이브
유형
코드
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.stream.Collectors;
class Solution {
class Food implements Comparable<Food> {
int index;
int time;
public Food(int index, int time) {
this.index = index;
this.time = time;
}
@Override
public int compareTo(Food other) {
return Integer.compare(this.time, other.time);
}
}
public int solution(int[] food_times, long k) {
int answer = 0;
long allFoodEatingTime = 0;
PriorityQueue<Food> foods = new PriorityQueue<>();
for (int i = 0; i < food_times.length; i++) {
foods.add(new Food(i + 1, food_times[i]));
allFoodEatingTime += food_times[i];
}
if(allFoodEatingTime <= k) return -1;
long totalTime = 0;
int foodLeft = foods.size();
int loopNum = 1;
while(totalTime + foodLeft <= k) {
totalTime += foodLeft;
while (foods.peek().time == loopNum) {
foods.poll();
foodLeft--;
}
loopNum++;
}
int findIndex = (int) (k - totalTime);
List<Food> foodsSortedByIndex = foods.stream().sorted(new Comparator<Food>() {
@Override
public int compare(Food o1, Food o2) {
return Integer.compare(o1.index, o2.index);
}
}).collect(Collectors.toList());
answer = foodsSortedByIndex.get(findIndex).index;
return answer;
}
}
참고: https://moonsbeen.tistory.com/m/371