Meeting Rooms II

本文介绍了一种算法,用于计算给定一系列会议时间安排时所需的最小会议室数量。通过自定义排序和优先队列来高效地解决该问题。
 1 /**
 2  * Definition for an interval.
 3  * public class Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() { start = 0; end = 0; }
 7  *     Interval(int s, int e) { start = s; end = e; }
 8  * }
 9  */
10 public class Solution {
11     public int minMeetingRooms(Interval[] intervals) {
12         if (intervals.length < 2) {
13             return intervals.length;
14         }
15         
16         Arrays.sort(intervals, new Comparator<Interval>() {
17             @Override
18             public int compare(Interval a, Interval b) {
19                 return a.start - b.start;
20             }
21         });
22         
23         PriorityQueue<Interval> queue = new PriorityQueue<>(intervals.length, new Comparator<Interval>() {
24            @Override
25            public int compare(Interval a, Interval b) {
26                return a.end - b.end;
27            }
28         });
29         
30         queue.offer(intervals[0]);
31         for (int i = 1; i < intervals.length; i++) {
32             Interval current = queue.poll();
33             if (intervals[i].start >= current.end) {
34                 current.end = intervals[i].end;
35             } else {
36                 queue.offer(intervals[i]);
37             }
38             queue.offer(current);
39         }
40         return queue.size();
41     }
42 }

 

转载于:https://www.cnblogs.com/shuashuashua/p/5748873.html

<think> 我们需要三个类:Building(基类)、House(派生类)、Office(派生类) 基类Building有保护成员:floors, rooms, square 派生类House:私有继承Building,新增私有成员bedrooms, balcony,并实现构造函数和show() 派生类Office:私有继承Building,新增私有成员phones, meeting_rooms,并实现构造函数和show() 注意:题目要求派生类House和Office分别有构造函数和show函数,用于初始化和显示数据。 但是,这里有一个问题:通常我们会使用公有继承,因为House和Office都是Building的一种。题目没有明确说明继承方式,但根据实际意义,应该使用公有继承。然而,题目中描述的是“建立类building的派生类house”,并没有指定继承方式,但按照常规,我们使用公有继承。 不过,题目中要求基类的成员是protected,这样在派生类中可以直接访问这些成员。 我们按照以下步骤: 1. 定义基类Building,包含保护成员floors, rooms, square,以及构造函数(可以带参数初始化)和show函数(虚函数,以便多态)。 2. 定义House类,公有继承Building,新增私有成员bedrooms, balcony,并定义构造函数和show函数(覆盖基类的show)。 3. 定义Office类,公有继承Building,新增私有成员phones, meeting_rooms,并定义构造函数和show函数(覆盖基类的show)。 注意:为了能够通过基类指针实现多态,我们将基类的show函数声明为虚函数。 但是,题目没有要求多态,但为了良好的设计,我们可以将show声明为虚函数。 由于题目要求显示派生类特有的成员,因此我们会在派生类的show函数中同时显示基类的成员和派生类新增的成员。 下面我们用C++代码实现: </think>以下是根据您的要求实现的C++代码,包含`Building`基类以及`House`和`Office`两个派生类: ```cpp #include <iostream> #include <string> using namespace std; // 基类 Building class Building { protected: int floors; // 层数 int rooms; // 房间数 double square; // 总面积(平方米) public: // 构造函数 Building(int f = 0, int r = 0, double s = 0.0) : floors(f), rooms(r), square(s) {} // 虚函数show(),允许派生类覆盖 virtual void show() const { cout << "Building Details:" << endl; cout << " Floors: " << floors << endl; cout << " Rooms: " << rooms << endl; cout << " Total Area: " << square << " m²" << endl; } }; // 派生类 House class House : public Building { private: int bedrooms; // 卧室数量 int balcony; // 阳台数量 public: // 构造函数 House(int f = 0, int r = 0, double s = 0.0, int bed = 0, int bal = 0) : Building(f, r, s), bedrooms(bed), balcony(bal) {} // 覆盖show()函数 void show() const override { cout << "House Details:" << endl; cout << " Floors: " << floors << endl; cout << " Total Rooms: " << rooms << endl; cout << " Total Area: " << square << " m²" << endl; cout << " Bedrooms: " << bedrooms << endl; cout << " Balconies: " << balcony << endl; } }; // 派生类 Office class Office : public Building { private: int phones; // 电话数量 int meeting_rooms; // 会议室数量 public: // 构造函数 Office(int f = 0, int r = 0, double s = 0.0, int p = 0, int m = 0) : Building(f, r, s), phones(p), meeting_rooms(m) {} // 覆盖show()函数 void show() const override { cout << "Office Details:" << endl; cout << " Floors: " << floors << endl; cout << " Total Rooms: " << rooms << endl; cout << " Total Area: " << square << " m²" << endl; cout << " Phones: " << phones << endl; cout << " Meeting Rooms: " << meeting_rooms << endl; } }; int main() { // 创建并展示House对象 House myHouse(2, 6, 150.5, 3, 2); myHouse.show(); cout << "\n-----------------\n" << endl; // 创建并展示Office对象 Office myOffice(5, 30, 1200.0, 25, 5); myOffice.show(); cout << "\n-----------------\n" << endl; // 使用基类指针演示多态 Building* buildings[2]; buildings[0] = new House(3, 8, 200.0, 4, 3); buildings[1] = new Office(10, 50, 3000.0, 40, 8); for (int i = 0; i < 2; i++) { buildings[i]->show(); delete buildings[i]; cout << endl; } return 0; } ``` ### 代码解释: **1. Building基类:** - **保护成员变量**: - `floors`:楼房的层数 - `rooms`:总房间数 - `square`:总面积(平方米) - **构造函数**:使用参数初始化所有成员变量 - **虚函数`show()`**:显示基本建筑信息,声明为`virtual`以支持多态 **2. House派生类:** - **新增私有成员**: - `bedrooms`:卧室数量 - `balcony`:阳台数量 - **构造函数**:初始化基类和新增成员 - **覆盖`show()`函数**:扩展显示卧室和阳台信息 **3. Office派生类:** - **新增私有成员**: - `phones`:电话数量 - `meeting_rooms`:会议室数量 - **构造函数**:初始化基类和新增成员 - **覆盖`show()`函数**:扩展显示电话和会议室信息 **4. 多态演示**: - 在`main()`函数中,使用`Building`指针数组存储不同派生类对象 - 通过基类指针调用`show()`时,会根据实际对象类型调用正确的实现 - 使用`delete`释放动态分配的内存 ### 程序输出示例: ``` House Details: Floors: 2 Total Rooms: 6 Total Area: 150.5 m² Bedrooms: 3 Balconies: 2 ----------------- Office Details: Floors: 5 Total Rooms: 30 Total Area: 1200 m² Phones: 25 Meeting Rooms: 5 ----------------- House Details: Floors: 3 Total Rooms: 8 Total Area: 200 m² Bedrooms: 4 Balconies: 3 Office Details: Floors: 10 Total Rooms: 50 Total Area: 3000 m² Phones: 40 Meeting Rooms: 8 ``` ### 关键设计特点: 1. **继承与多态**:使用公有继承和虚函数实现运行时多态 2. **保护成员**:基类成员设为`protected`,允许派生类直接访问 3. **构造函数链**:派生类构造函数通过初始化列表调用基类构造函数 4. **方法覆盖**:派生类覆盖`show()`方法提供特定实现 5. **内存管理**:演示了动态内存分配和释放的正确用法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值