[Python] Meeting Rooms III (LeetCode) [closed]

Discussão em 'Python' iniciado por Stack, Janeiro 26, 2025.

  1. Stack

    Stack Membro Participativo

    I was trying to solve Meeting Rooms III on LeetCode and my solution only passes 60/80 testcases.


    Meeting Rooms III: You are given an integer n. There are n rooms numbered from 0 to n - 1.

    You are given a 2D integer array meetings where meetings = [starti, endi] means that a meeting will be held during the half-closed time interval [starti, endi). All the values of starti are unique.

    Meetings are allocated to rooms in the following manner:

    Each meeting will take place in the unused room with the lowest number. If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the same duration as the original meeting. When a room becomes unused, meetings that have an earlier original start time should be given the room. Return the number of the room that held the most meetings. If there are multiple rooms, return the room with the lowest number.

    A half-closed interval [a, b) is the interval between a and b including a and not including b.
    ​


    MY SOLUTION:

    class Solution:
    def mostBooked(self, n: int, meetings: List\[List\[int\]\]) -\> int:
    meetings.sort()

    available = [i for i in range(n)]
    used = []
    count = [0] * n

    for start, end in meetings:
    while used and start >= used[0]:
    _, room = heapq.heappop(used)
    heapq.heappush(available, room)
    if not available:
    prevEnd, room = heapq.heappop(used)
    heapq.heappush(used, (prevEnd + (end - start), room))
    else:
    room = heapq.heappop(available)
    heapq.heappush(used, (end, room))

    return count.index(max(count))


    The reasoning GPT gave me for why I wasn't passing was:


    Your code does not pass the test because it does not match the problem's expected scheduling policy. In your code, if the earliest finishing room (the heap's root) is still busy, you immediately open a new room (as long as you have not used all n). Once you reach n rooms, you reuse the earliest finishing room. This approach is logically consistent with a certain policy, but the problem's tests expect a different assignment of meetings.

    Typical solutions free rooms as soon as they finish and assign the next meeting to the smallest-index available room. This results in a different final "usage count" per room, so your answer to "most used room" does not match what the test expects. The test case is specifically designed to fail if you keep creating new rooms instead of reusing a room that is actually free.
    ​

    I'm still failing to fully understand why my code doesn't work. I get that the issue is with how I'm processing which rooms are being used and which ones are free. However, I feel like my solution follows what the instructions are asking (and I don't feel like the instructions are specific enough). I guess what I'm trying to ask is, is that just how this problem is and do I basically just have to memorize the problem? Just putting this out for thoughts.

    Continue reading...

Compartilhe esta Página