Leetcode-3169. Count Days Without Meetings 题解

Intuition

To get meeting count, we just need to get dis-joint continous segments where meeting days are contained.

First sort this array, with order:
1.a[0] == b[0], compare a[1] > b[1]
2.a[0] < b[0], return -1;
3.a[0] > b[1], return 1;

Then travel in the sorted 2D array:

  1. meeting[i+1][0] > meeting[i][1]: means the new segment started, add a segment to result.
  2. meeting[i+1][0] <= meeting[i][1]:
    2.1 if meeting[i+1][1] > meeting[i][1]: merge the prev segment’s end to meeting[i+1][1], in other words, enlarge the prev segment.
    2.2 if meeting[i+1][1] > meeting[i][1]: means the prev segment contains the current segment, do nothing.
  3. meeting[i+1][0] == meeting[i][1]: means the prev segment is adjacent to current segment, just check whether meeting[i+1][1] > meeting[i][1], if so, merge the prev segment’s end to meeting[i+1][1].

Total Time complexity is sorting cost: O(n*logn)

Approach

Complexity

  • Time complexity:
  • O(nlogn)
  • Space complexity:
  • O(n): The worst case is every segment length is 1 and all elements are in segments.

Code

// Runtime 61 ms Beats 100.00% of users with Java
// Memory 99.61 MB Beats 100.00% of users with Java
// Sort And get all dis-joint continous segment.
// T:O(nlogn), S:(n)
// 
class Solution {
    public int countDays(int days, int[][] meetings) {
        Arrays.sort(meetings, (a, b) -> a[0] != b[0] ? (a[0] - b[0]) : (a[1] != b[1] ? (a[1] - b[1]) : 0));
        List<List<Integer>> segment = new ArrayList<>();
        for (int[] meeting : meetings) {
            if (segment.isEmpty()) {
                List<Integer> list = new ArrayList<>();
                list.add(meeting[0]);
                list.add(meeting[1]);
                segment.add(list);
            } else {
                List<Integer> list = segment.get(segment.size() - 1);
                if (meeting[0] == list.get(1)) {
                    list.set(1, meeting[1]);
                } else if (meeting[0] < list.get(1)) {
                    if (meeting[1] > list.get(1)) {
                        list.set(1, meeting[1]);
                    }
                } else {
                    List<Integer> list2 = new ArrayList<>();
                    list2.add(meeting[0]);
                    list2.add(meeting[1]);
                    segment.add(list2);
                }
            }
        }
        int count = 0;
        for (List<Integer> segmentItem : segment) {
            count += segmentItem.get(1) - segmentItem.get(0) + 1;
        }

        return days - count;
    }
}


相关推荐

  1. Leetcode-3169. Count Days Without Meetings 题解

    2024-06-06 06:06:04       36 阅读
  2. leetcode 316. 去除重复字母

    2024-06-06 06:06:04       34 阅读
  3. Leetcode-316-去除重复字母

    2024-06-06 06:06:04       29 阅读
  4. Leetcode 3139. Minimum Cost to Equalize Array

    2024-06-06 06:06:04       34 阅读
  5. Leetcode 3149. Find the Minimum Cost Array Permutation

    2024-06-06 06:06:04       33 阅读
  6. leetcode中shell题解

    2024-06-06 06:06:04       61 阅读
  7. LeetCode每日一题】单调栈316去除重复字母

    2024-06-06 06:06:04       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 06:06:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 06:06:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 06:06:04       82 阅读
  4. Python语言-面向对象

    2024-06-06 06:06:04       91 阅读

热门阅读

  1. 函数也能当变量?Python一等函数让你大开眼界!

    2024-06-06 06:06:04       27 阅读
  2. Spring Bean参数校验Validator

    2024-06-06 06:06:04       31 阅读
  3. Apache Calcite - 使用内置函数

    2024-06-06 06:06:04       31 阅读
  4. json.dumps参数

    2024-06-06 06:06:04       31 阅读
  5. ArrayList和LinkedList对比,ArrayList使用注意事项

    2024-06-06 06:06:04       28 阅读
  6. linux中基于docker安装RabbitMQ。

    2024-06-06 06:06:04       30 阅读
  7. 学习笔记 SD卡(1)

    2024-06-06 06:06:04       23 阅读
  8. UOS开通22端口用于SSH

    2024-06-06 06:06:04       27 阅读
  9. 阿里云盘手机批量修改文件名

    2024-06-06 06:06:04       25 阅读