230308 무지의 먹방 라이브

Jongleee·2023년 3월 8일
0

TIL

목록 보기
200/737
public static class Food {
    private final int index;
    private int time;

    public Food(int index, int time) {
        this.index = index;
        this.time = time;
    }

    public int getIndex() {
        return index;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }
}

public static int solution(int[] food_times, long k) {
    Queue<Food> q = new PriorityQueue<>(Comparator.comparingInt(Food::getTime).thenComparingInt(Food::getIndex));

    for (int i = 0; i < food_times.length; i++) {
        q.add(new Food(i + 1, food_times[i]));
    }

    int prevTime = 0;
    int n = food_times.length;

    while (!q.isEmpty()) {
        Food food = q.peek();
        int diff = food.getTime() - prevTime;
        long totalTime = (long) diff * n;

        if (totalTime <= k) {
            k -= totalTime;
            prevTime = food.getTime();
            q.poll();
            n--;
        } else {
            break;
        }
    }

    if (q.isEmpty()) {
        return -1;
    }

    List<Food> remainingFoods = new ArrayList<>();

    while (!q.isEmpty()) {
        remainingFoods.add(q.poll());
    }

    remainingFoods.sort(Comparator.comparingInt(Food::getIndex));

    int index = (int) (k % n);
    return remainingFoods.get(index).getIndex();
}

0개의 댓글