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) */
当你看清人们的真相,于是你知道了,你可以忍受孤独
分类:
数据结构与算法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构