231020 무지의 먹방 라이브

Jongleee·2023년 10월 20일
0

TIL

목록 보기
395/737
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 int solution(int[] foodTimes, long k) {
	Queue<Food> foodQueue = new PriorityQueue<>(
			Comparator.comparingInt(Food::getTime).thenComparingInt(Food::getIndex));

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

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

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

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

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

	List<Food> remainingFoods = new ArrayList<>(foodQueue);
	remainingFoods.sort(Comparator.comparingInt(Food::getIndex));

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

출처:https://school.programmers.co.kr/learn/courses/30/lessons/42891

0개의 댓글