Leetcode 855 考场就坐

 

  JAVA 红黑树:

class ExamRoom {
        TreeSet<Integer> seats;
        int last;

        public ExamRoom(int N) {
            this.last = N - 1;
            this.seats = new TreeSet<Integer>();
        }

        public final int seat() {
            if (seats.size() == 0) return setAndReturn(0);
            int dist = 0, position = 0, first = seats.first(), pre = first, last = seats.last();
            if (first == last) {
                if (first >= this.last - first) return setAndReturn(0);
                else return setAndReturn(this.last);
            }
            for (Integer seat : this.seats) {
                if (seat - pre > 1) {
                    int curr = (seat - pre) / 2;
                    if (curr > dist) {
                        dist = curr;
                        position = pre + curr;
                    }
                }
                pre = seat;
            }
            if (first != 0 && first - 0 >= dist) {
                dist = first;
                position = 0;
            }
            if (last != this.last && this.last - last > dist) position = this.last;
            return setAndReturn(position);
        }

        private final int setAndReturn(int position) {
            this.seats.add(position);
            return position;
        }

        public final void leave(int p) {
            this.seats.remove(p);
        }
    }

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

  JS 数组,空间复杂度 O(N),不符合题目要求:

/**
 * @param {number} N
 */
var ExamRoom = function (N) {
    this.last = N - 1;
    this.students = new Array(N);
    for (let i = 0; i < N; i++) this.students[i] = 0;
};

/**
 * @return {number}
 */
ExamRoom.prototype.seat = function () {
    let first = this.getFirst(), last = this.getLast(), pre = first, dist = 0, position = 0;
    for (let i = first + 1; i <= last; i++) {
        if (!this.students[i]) continue;
        let currDist = parseInt((i - pre) / 2);
        if (currDist > dist) {
            dist = currDist;
            position = pre + currDist;
        }
        pre = i;
    }
    if (first != 0 && !first) return this.seatAndReturn(0);
    if (first >= dist) {
        dist = first;
        position = 0;
    }
    if (this.last - last > dist) position = this.last;
    return this.seatAndReturn(position);
};

ExamRoom.prototype.seatAndReturn = function (seat) {
    this.students[seat] = 1;
    return seat;
}

ExamRoom.prototype.getFirst = function () {
    for (let i = 0; i <= this.last; i++) {
        if (this.students[i]) return i;
    }
}

ExamRoom.prototype.getLast = function () {
    for (let i = this.last; i >= 0; i--) {
        if (this.students[i]) return i;
    }
}

/**
 * @param {number} p
 * @return {void}
 */
ExamRoom.prototype.leave = function (p) {
    this.students[p] = 0;
};

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

  JS:

/**
 * @param {number} N
 */
var ExamRoom = function (N) {
    this.students = [];
    this.last = N - 1;
};

ExamRoom.prototype.insert = function (seat) {
    this.students.push(seat);
    this.students.sort(this.sort);
}

ExamRoom.prototype.sort=function(a, b) {
  return a - b;
}
/**
 * @return {number}
 */
ExamRoom.prototype.seat = function () {
    if (this.students.length == 0) return this.seatAndReturn(0);
    let first = this.students[0], len = this.students.length, dist = 0, pre = first, position = 0;
    for (let i = 1; i < len; i++) {
        let currDist = parseInt((this.students[i] - pre) / 2);
        if (currDist > dist) {
            position = pre + currDist;
            dist = currDist;
        }
        pre = this.students[i];
    }
    if (first >= dist) {
        dist = first;
        position = 0;
    }
    if ((this.last - pre) > dist) position = this.last;
    return this.seatAndReturn(position);
};

ExamRoom.prototype.seatAndReturn = function (seat) {
    this.insert(seat);
    return seat;
}

/**
 * @param {number} p
 * @return {void}
 */
ExamRoom.prototype.leave = function (p) {
    for (let i = 0; i < this.students.length; i++) {
        if (this.students[i] == p) return this.students.splice(i, 1);
    }
};

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

 

posted @ 2021-03-03 21:02  牛有肉  阅读(105)  评论(0编辑  收藏  举报