1. 程式人生 > >855. Exam Room(python+cpp)

855. Exam Room(python+cpp)

題目:

In an exam room, there are N seats in a single row, numbered 0, 1, 2, …, N-1 . When a student enters the room, they must sit in the seat that maximizes the distance to the closest person. If there are multiple such seats, they sit in the seat with the lowest number. (Also, if no one is in the room, then the student sits at seat number 0.) Return a class ExamRoom(int N)

that exposes two functions: ExamRoom.seat() returning an int representing what seat the student sat in, and ExamRoom.leave(int p)representing that the student in seat number p now leaves the room. It is guaranteed that any calls to ExamRoom.leave(p) have a student sitting in seat p. Example 1:

Input: ["ExamRoom","seat","seat","seat","seat","leave","seat"], [[10],[],[],[],[],[4],[]] 
Output: [null,0,9,4,2,null,5] 
Explanation:
ExamRoom(10) -> null 
seat() -> 0, no one is in the room, then the student sits at seat number 0. 
seat() -> 9, the student sits at the last seat number 9. 
seat() -> 4, the student sits at the last seat number 4. 
seat() -> 2, the student sits at the last seat number 2.
leave(4) -> null 
seat() -> 5, the student sits at the last seat number 5. ​​​​​​​

Note:

1 <= N <= 10^9 ExamRoom.seat()and ExamRoom.leave()will be called at most 10^4 times across all test cases. Calls to ExamRoom.leave(p) are guaranteed to have a student currently sitting in seat number p.

解釋:849. Maximize Distance to Closest Person(python+cpp)821. Shortest Distance to a Character(python+cpp)

類似,但是是實現一個類,而且不是返回最大距離而是返回應該坐下的位置的index如果有多個位置可以坐下,返回最小的index。 用一個數組seats來記錄有人做的位置的index,對於陣列seats錯位求距離,如果距離大於當前的最大距離,則這兩個位置中間的位置就是一個可以坐的位置,插入陣列中,注意,陣列要隨時保持排序。 python程式碼:

import bisect
class ExamRoom(object):

    def __init__(self, N):
        """
        :type N: int
        """
        #seats用於儲存有人坐的位置的index
        self.seats=[]
        self.N=N
        
    def seat(self):
        """
        :rtype: int
        """   
        if not self.seats:
            res=0
        else:
            d,res=self.seats[0],0 
            for a, b in zip(self.seats,self.seats[1:]):
                if (b-a)/2>d:
                    d=(b-a)/2
                    res=(a+b)/2
            if self.N-1-self.seats[-1]>d:
                res=self.N-1
        bisect.insort(self.seats,res)
        return res
        
    def leave(self, p):
        """
        :type p: int
        :rtype: void
        """
        self.seats.remove(p)

# Your ExamRoom object will be instantiated and called as such:
# obj = ExamRoom(N)
# param_1 = obj.seat()
# obj.leave(p)

c++程式碼:

class ExamRoom {
public:
    int global_N;
    vector<int>seats;
    ExamRoom(int N) {
        global_N=N;  
    }
    
    int seat() {
        int res;
        if (seats.size()==0)
            res=0;
        else
        {
            int d=seats[0];
            res=0;
            for (int i=0;i<seats.size()-1;i++)
            {
                int a=seats[i],b=seats[i+1];
                if((b-a)/2>d)
                {
                    d=(b-a)/2;
                    res=(a+b)/2;
                }
            }
            if (global_N-1-seats[seats.size()-1]>d)
                res=global_N-1;
        }
        //尋找第一個大於res的數的
        auto index=lower_bound(seats.begin(),seats.end(),res);
        seats.insert(index,res);
        return res;
        
    }
    
    void leave(int p) {
        for (vector<int>::iterator iter = seats.begin(); iter != seats.end();)
        {
            if (*iter==p)
            {
                iter = seats.erase(iter);
                break;
            }
            else
                iter++;
        }
    }
};

/**
 * Your ExamRoom object will be instantiated and called as such:
 * ExamRoom obj = new ExamRoom(N);
 * int param_1 = obj.seat();
 * obj.leave(p);
 */

總結: 注意c++中刪除指定元素的寫法(python直接用remove()就可以刪除list中指定元素的第一個匹配項)

for (vector<int>::iterator iter = lst.begin(); iter != lst.end();)
{
    if (符合條件)
    {
        iter = lst.erase(iter);
    }
    else
    {
        iter++;
    }
}

lower_bound()返回大於指定元素的第一個值的指標,所以直接在這裡插入就好,插入的時候也是傳入指標。