LeetCode | 732. My Calendar III 区域覆盖技巧题

本文介绍了一种实现MyCalendarThree类的方法,该类用于存储事件并返回最大重叠预订数。通过具体示例展示了如何使用该类进行预订,并解释了背后的逻辑及数据结构的选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Implementa MyCalendarThree class to store your events. A new event can always beadded.

Yourclass will have one method, book(int start, int end). Formally, this represents a bookingon the half open interval [start, end), the range of real numbers x such that start <= x < end.

A K-booking happenswhen K events have some non-empty intersection (ie., there is sometime that is common to all K events.)

Foreach call to the method MyCalendar.book, return an integer K representing the largest integer such that there exists a K-booking in the calendar.

Your class will be called like this: MyCalendarThreecal = new MyCalendarThree(); MyCalendarThree.book(start, end)

Example1:

MyCalendarThree();

MyCalendarThree.book(10, 20); // returns 1

MyCalendarThree.book(50, 60); // returns 1

MyCalendarThree.book(10, 40); // returns 2

MyCalendarThree.book(5, 15); // returns 3

MyCalendarThree.book(5, 10); // returns 3

MyCalendarThree.book(25, 55); // returns 3

Explanation:

The first two events can be booked and are disjoint, so themaximum K-booking is a 1-booking.

The third event [10, 40) intersects the first event, and themaximum K-booking is a 2-booking.

The remaining events cause the maximum K-booking to be only a3-booking.

Note that the last event locally causes a 2-booking, but theanswer is still 3 because

eg. [10, 20), [10, 40), and [5, 15) are still triple booked.

Note:

· The number of calls to MyCalendarThree.book pertest case will be at most 400.

· In calls to MyCalendarThree.book(start, end)start and end areintegers in the range [0, 10^9].

通过这一题我学会了两点,其一是在编程之前一定要用笔在纸上画出代码的结构,其二是当对map使用map[a]=b,的时候map会自动将里面的值按照a的值大小排序,以后在做一个数组,需要不断的向里面添加值,且添加的值需要插入的时候,记得使用map,因为会自动排序

以后在做这种多个区域叠加的题目的时候记得利用这种数据结构,因为这种数据结构当从左向右遍历的时候能够得到任何一个点的重叠次数

其实这一题还有一个简单版本,其实使用map会简单很多,因为不用手动排序,会自动排序

class MyCalendarThree {

public:

      MyCalendarThree(){

           

      }

      vector<pair<int,int>>store;

     

      intbook(int start, int end) {

            intongoint = 0; int result = 0;

            intstartMark=-1, endMark=-1;

            for(int i = 0; i < store.size(); i++)

            {

                  if(store[i].first < start)

                  {

                       result= max(result, (ongoint += store[i].second));

                  }

                  else

                  if(store[i].first==start)

                  {

                       startMark= 1;

                       store[i].second++;

                       result= max(result, (ongoint += store[i].second));

                  }

                  elseif (store[i].first == end)

                  {

                       if(startMark == -1)

                       {

                             startMark= 1;

                             store.insert(store.begin()+ i, pair<int, int>(start, 1));

                             result= max(result, (ongoint += store[i].second));

                       }

                       else

                       {

                             endMark= 1;

                             store[i].second--;

                             result= max(result, (ongoint += store[i].second));

                       }

                  }

                  elseif(store[i].first>start&&store[i].first<end)

                  {

                       if(startMark == -1)

                       {

                             startMark= 1;

                             store.insert(store.begin()+ i, pair<int, int>(start, 1));

                             result= max(result, (ongoint += store[i].second));

                        }

                       else

                       {

                             result= max(result, (ongoint += store[i].second));

                       }

                  }

                  else

                  {

                       if(startMark == -1)

                       {

                             startMark= 1;

                             store.insert(store.begin()+ i, pair<int, int>(start, 1));

                             result= max(result, (ongoint += store[i].second));

                       }

                       else

                       if(endMark == -1)

                       {

                             endMark= 1;

                             store.insert(store.begin()+ i, pair<int, int>(end, -1));

                             result= max(result, (ongoint += store[i].second));

                       }

                       else

                       {

                             result= max(result, (ongoint += store[i].second));

                       }

                  }

            }

            if(startMark == -1)

            {

                  store.push_back(pair<int,int>(start, 1));

                  result= max(result, (ongoint += store[store.size()-1].second));

            }

            if(endMark == -1)

            {

                  store.push_back(pair<int,int>(end, -1));

                  result= max(result, (ongoint += store[store.size() - 1].second));

            }

 

            returnresult;

      }

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值