We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
这是一道模拟题,这是我第二次遇见了,第一次是Time to cross the bridge。
这类题型的solution大体上都是利用1个或以上的Heap进行模拟。特别是这种对于时间和排队等待的类型,Heap里存的是时间。
这题不难,但我还是花了44分钟左右AC了,要加快速度,有以下注意点:
while (!roomsPQ.isEmpty() && roomsPQ.peek().time <= meeting[0]) {
time<=
class Solution { public int mostBooked(int n, int[][] meetings) { Arrays.sort(meetings, Comparator.comparingInt(a -> a[0])); PriorityQueue<Pair> roomsPQ = new PriorityQueue<>(new Comparator<Pair>() { @Override public int compare(Pair o1, Pair o2) { if (o1.time == o2.time) { return o1.idx - o2.idx; } return (int) (o1.time - o2.time); } }); for (int i = 0; i < n; ++i) { roomsPQ.add(new Pair(0, i, 0)); } // simulate for (int[] meeting : meetings) { if (roomsPQ.peek().time >= meeting[0]) { Pair room = roomsPQ.poll(); room.time += meeting[1] - meeting[0]; room.cnt++; roomsPQ.add(room); } else { PriorityQueue<Pair> tmp = new PriorityQueue<>(Comparator.comparing(o -> o.idx)); while (!roomsPQ.isEmpty() && roomsPQ.peek().time <= meeting[0]) { tmp.add(roomsPQ.poll()); } Pair room = tmp.poll(); room.time = meeting[1]; room.cnt++; roomsPQ.add(room); while (!tmp.isEmpty()) { roomsPQ.add(tmp.poll()); } } } int ans = -1, max = 0; while (!roomsPQ.isEmpty()) { Pair room = roomsPQ.poll(); if (room.cnt > max) { max = room.cnt; ans = room.idx; } else if (room.cnt == max) { ans = Math.min(ans, room.idx); } } return ans; } class Pair { long time; int idx; int cnt; public Pair(long time, int idx, int cnt) { this.time = time; this.idx = idx; this.cnt = cnt; } } }
类似题目:
The text was updated successfully, but these errors were encountered:
No branches or pull requests
这是一道模拟题,这是我第二次遇见了,第一次是Time to cross the bridge。
这类题型的solution大体上都是利用1个或以上的Heap进行模拟。特别是这种对于时间和排队等待的类型,Heap里存的是时间。
这题不难,但我还是花了44分钟左右AC了,
要加快速度,有以下注意点:while (!roomsPQ.isEmpty() && roomsPQ.peek().time <= meeting[0]) {
中time<=
,别忘了等号,因为题目条件是半区间,所以等于meeting时间的情况下rooms还是可以用的,要加入排序类似题目:
The text was updated successfully, but these errors were encountered: