Leetcode 855. Exam Room 考场就座:提供两种解法

Leetcode 855. Exam Room 考场就座: 提供两种解法

    • 855. Exam Room 考场就座(两种解法)
      • 题目描述
      • 示例:
      • 解答1
      • 代码1
      • 解答2
      • 代码2

855. Exam Room 考场就座(两种解法)

题目描述

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§ have a student sitting in seat p.

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§ are guaranteed to have a student currently sitting in seat number 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.

​​​​​​​

解答1

第一种方法用一个vector来存储就座人的座位号,每次就座的时候,遍历数组找到最大的座位间隔,然后在这个间隔的中间位置就座。

离座的时候只需从vector中删除这个座位号即可。这个算法复杂度为线性的。

代码1

class ExamRoom {
int num = 0;
vector<int > row;
public:
    ExamRoom(int N) {
        num = N;
    }
    
    int seat() {
        if (row.size() == 0) {
        	row.push_back(0);
        	return 0;
        }
        int d = max(row[0], num - 1 - row[row.size() - 1]);
        for (int i = 1;i < row.size(); i ++) {
        	d = max(d, (row[i] - row[i - 1]) / 2);
        }
        if (d == row[0]) {
        	row.insert(row.begin(), 0);
            return 0;
        }
        else {
        	for (int i = 1; i < row.size(); i++) {
        		if ((row[i] - row[i - 1]) / 2 == d) {
        			row.insert(row.begin() + i, (row[i] + row[i - 1]) / 2);
        			return row[i];
        		}
        	}
        }
        row.push_back(num - 1);
        return num - 1;
    }
    
    void leave(int p) {
        for (int i = 0; i < row.size(); i++) {
        	if (row[i] == p)
        		row.erase(row.begin() + i);
        }
    }
};

解答2

第二种想法算法复杂度为 O ( l o g n ) O(logn) O(logn),在全局中maintain一个set数组,里面按照座位间隔从大到小排列,每次就座的时候就把这个set数据的第一个位置的间隔拿掉,然后将这个间隔一分为二再插入到这个set数据中。

在离座的时候,在set数组里面找到离座的人涉及到的两个间隔,把这两个间隔合并之后再插入到set中就完成了离座。

代码2

struct IntV
{
	static int N;
	int l, r;
	IntV(int l, int r) : l(l), r(r) {};
	int getDistance() const {
		if (l == 0) return r;
		if (r == N - 1) return N - 1 - l;
		if (r < l) return -1;
		else return (r - l) / 2;		
	} 

	int getInsertPoint() const {
		if (l == 0) return 0;
		if (r == N  - 1) return N - 1;
		else return (l + r) / 2; 
	}
	int operator < (const IntV& a) const {
		int d1 = getDistance();
		int d2 = a.getDistance();
		if(d1 != d2) return d1 > d2;
        	return l < a.l;
	}
};
int IntV :: N = 0;
class ExamRoom{
public:
	set<IntV> interval;
	map<int, int> r2l, l2r;
	ExamRoom(int N) {
		IntV::N = N;
		interval.clear(); l2r.clear(); r2l.clear();
		interval.insert(IntV(0, N - 1));
		l2r[0] = N - 1;
		r2l[N -1] = 0;
	}

	int seat() {
		auto cur = *(interval.begin());
		interval.erase(interval.begin());
		int pos = cur.getInsertPoint();
		cout << pos;
		interval.insert(IntV(cur.l, pos - 1));
		interval.insert(IntV(pos + 1, cur.r));
		l2r[cur.l] = pos - 1;
		r2l[pos - 1] = cur.l;
		l2r[pos + 1] = cur.r;
		r2l[cur.r] = pos + 1;
		return pos;

	}
	void leave(int p) {
		auto r = l2r[p + 1];
		auto l = r2l[p - 1];
		interval.erase(IntV(l, p - 1));
		interval.erase(IntV(p + 1, r));
		interval.insert(IntV(l, r));
		l2r[l] = r;
		r2l[r] = l;
		r2l.erase(p - 1);
		l2r.erase(p + 1);
	}
};

你可能感兴趣的:(leetcode,优先队列)