• 【C语言刷LeetCode】731. 我的日程安排表 II(M)


    实现一个 MyCalendar 类来存放你的日程安排。如果要添加的时间内不会导致三重预订时,则可以存储这个新的日程安排。

    MyCalendar 有一个 book(int start, int end)方法。它意味着在 start 到 end 时间内增加一个日程安排,注意,这里的时间是半开区间,即 [start, end), 实数 x 的范围为,  start <= x < end。

    当三个日程安排有一些时间上的交叉时(例如三个日程安排都在同一时间内),就会产生三重预订。

    每次调用 MyCalendar.book方法时,如果可以将日程安排成功添加到日历中而不会导致三重预订,返回 true。否则,返回 false 并且不要将该日程安排添加到日历中。

    请按照以下步骤调用MyCalendar 类: MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)

    示例:

    MyCalendar();
    MyCalendar.book(10, 20); // returns true
    MyCalendar.book(50, 60); // returns true
    MyCalendar.book(10, 40); // returns true
    MyCalendar.book(5, 15); // returns false
    MyCalendar.book(5, 10); // returns true
    MyCalendar.book(25, 55); // returns true
    解释: 
    前两个日程安排可以添加至日历中。 第三个日程安排会导致双重预订,但可以添加至日历中。
    第四个日程安排活动(5,15)不能添加至日历中,因为它会导致三重预订。
    第五个日程安排(5,10)可以添加至日历中,因为它未使用已经双重预订的时间10。
    第六个日程安排(25,55)可以添加至日历中,因为时间 [25,40] 将和第三个日程安排双重预订;
    时间 [40,50] 将单独预订,时间 [50,55)将和第二个日程安排双重预订。
     

    提示:

    每个测试用例,调用 MyCalendar.book 函数最多不超过 1000次。
    调用函数 MyCalendar.book(start, end)时, start 和 end 的取值范围为 [0, 10^9]。

    来源:力扣(LeetCode)
    链接:https://leetcode.cn/problems/my-calendar-ii
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    相比较于729题,731是其升级版,判断三重预定。

    类似思路,分别用两组数组分别存放可以预定的起始和一次冲突的预定。

    注意一次冲突的数组中,我们不关系是否有重复元素,反正最多1000次。

    同时无论有没冲突,我们都把可以的预定存入数组。

    代码如下:

    1. typedef struct {
    2. int flarr[1000];
    3. int frarr[1000];
    4. int slarr[1000];
    5. int srarr[1000];
    6. } MyCalendarTwo;
    7. int fidx;
    8. int sidx;
    9. MyCalendarTwo* myCalendarTwoCreate() {
    10. MyCalendarTwo *obj = (MyCalendarTwo *)malloc(sizeof(MyCalendarTwo));
    11. fidx = 0;
    12. sidx = 0;
    13. return obj;
    14. }
    15. bool myCalendarTwoBook(MyCalendarTwo* obj, int start, int end) {
    16. int i;
    17. for (i = 0; i < sidx; i++) { // 和二重预定数组比较
    18. if (start < obj->srarr[i] && end > obj->slarr[i]) { // 这种肯定就冲突了
    19. return false;
    20. }
    21. }
    22. // 新增的二重预定可能和以前二重预定一样,但还是直接加进去,重复就重复
    23. for (i = 0; i < fidx; i++) {
    24. if (start < obj->frarr[i] && end > obj->flarr[i]) { // 有冲突,放入二重预定数组里面
    25. obj->slarr[sidx] = fmax(start, obj->flarr[i]); // 注意边界处理
    26. obj->srarr[sidx] = fmin(end, obj->frarr[i]);
    27. sidx++;
    28. }
    29. }
    30. // 放入一重预定数组里,无论有没有冲突
    31. obj->flarr[fidx] = start;
    32. obj->frarr[fidx] = end;
    33. fidx++;
    34. return true;
    35. }
    36. void myCalendarTwoFree(MyCalendarTwo* obj) {
    37. free(obj);
    38. }
    39. /**
    40. * Your MyCalendarTwo struct will be instantiated and called as such:
    41. * MyCalendarTwo* obj = myCalendarTwoCreate();
    42. * bool param_1 = myCalendarTwoBook(obj, start, end);
    43. * myCalendarTwoFree(obj);
    44. */

  • 相关阅读:
    MindSpore编译时报错
    Ubuntu1804.5安装后的基本配置
    为提高 SDLC 安全,GitHub 发布新功能|GitHub Universe 2022
    Node.js | 基础完结、综合训练 —— 路由应用实战教程
    基于无人机的物联网空基中继鲁棒优化
    手机注册.
    Java电子招投标采购系统源码-适合于招标代理、政府采购、企业采购、等业务的企业
    Python连本地MySQL接数据库
    基于VUE的酒店管理系统的设计与实现
    SYS——汽车零部件从项目到软件开发过程
  • 原文地址:https://blog.csdn.net/jin615567975/article/details/125884078