|
| 1 | +--- |
| 2 | +title: 3169.无需开会的工作日:排序+一次遍历——不需要正难则反,因为正着根本不难 |
| 3 | +date: 2025-07-12 22:33:58 |
| 4 | +tags: [题解, LeetCode, 中等, 数组, 排序] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +--- |
| 7 | + |
| 8 | +# 【LetMeFly】3169.无需开会的工作日:排序+一次遍历——不需要正难则反,因为正着根本不难 |
| 9 | + |
| 10 | +力扣题目链接:[https://leetcode.cn/problems/count-days-without-meetings/](https://leetcode.cn/problems/count-days-without-meetings/) |
| 11 | + |
| 12 | +<p>给你一个正整数 <code>days</code>,表示员工可工作的总天数(从第 1 天开始)。另给你一个二维数组 <code>meetings</code>,长度为 <code>n</code>,其中 <code>meetings[i] = [start_i, end_i]</code> 表示第 <code>i</code> 次会议的开始和结束天数(包含首尾)。</p> |
| 13 | + |
| 14 | +<p>返回员工可工作且没有安排会议的天数。</p> |
| 15 | + |
| 16 | +<p><strong>注意:</strong>会议时间可能会有重叠。</p> |
| 17 | + |
| 18 | +<p> </p> |
| 19 | + |
| 20 | +<p><strong class="example">示例 1:</strong></p> |
| 21 | + |
| 22 | +<div class="example-block"> |
| 23 | +<p><strong>输入:</strong><span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> |
| 24 | + |
| 25 | +<p><strong>输出:</strong><span class="example-io">2</span></p> |
| 26 | + |
| 27 | +<p><strong>解释:</strong></p> |
| 28 | + |
| 29 | +<p>第 4 天和第 8 天没有安排会议。</p> |
| 30 | +</div> |
| 31 | + |
| 32 | +<p><strong class="example">示例 2:</strong></p> |
| 33 | + |
| 34 | +<div class="example-block"> |
| 35 | +<p><strong>输入:</strong><span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> |
| 36 | + |
| 37 | +<p><strong>输出:</strong><span class="example-io">1</span></p> |
| 38 | + |
| 39 | +<p><strong>解释:</strong></p> |
| 40 | + |
| 41 | +<p>第 5 天没有安排会议。</p> |
| 42 | +</div> |
| 43 | + |
| 44 | +<p><strong class="example">示例 3:</strong></p> |
| 45 | + |
| 46 | +<div class="example-block"> |
| 47 | +<p><strong>输入:</strong><span class="example-io">days = 6, meetings = [[1,6]]</span></p> |
| 48 | + |
| 49 | +<p><strong>输出:</strong>0</p> |
| 50 | + |
| 51 | +<p><strong>解释:</strong></p> |
| 52 | + |
| 53 | +<p>所有工作日都安排了会议。</p> |
| 54 | +</div> |
| 55 | + |
| 56 | +<p> </p> |
| 57 | + |
| 58 | +<p><strong>提示:</strong></p> |
| 59 | + |
| 60 | +<ul> |
| 61 | + <li><code>1 <= days <= 10<sup>9</sup></code></li> |
| 62 | + <li><code>1 <= meetings.length <= 10<sup>5</sup></code></li> |
| 63 | + <li><code>meetings[i].length == 2</code></li> |
| 64 | + <li><code>1 <= meetings[i][0] <= meetings[i][1] <= days</code></li> |
| 65 | +</ul> |
| 66 | + |
| 67 | +好奇,怎么都在说正难则反。 |
| 68 | + |
| 69 | +## 解题方法:排序 |
| 70 | + |
| 71 | +只需要按照meetings开始的顺序从小到大排序,使用一个变量(last)记录上次会议的结束日期(初始值为0),接着开始遍历meetings数组。 |
| 72 | + |
| 73 | +如果开始时间比last晚不只一天,就说明从last到这个开始时间都有空,累加到答案中。每遍历完一个meeting,就将last更新为last和meeting结束时间的最大值。 |
| 74 | + |
| 75 | +最终,days-last也是空闲时间,累加到答案中。 |
| 76 | + |
| 77 | ++ 时间复杂度$O(n\log n)$,其中$n=len(meetings)$。 |
| 78 | ++ 空间复杂度$O(\log n)$ |
| 79 | + |
| 80 | +### AC代码 |
| 81 | + |
| 82 | +#### C++ |
| 83 | + |
| 84 | +```cpp |
| 85 | +/* |
| 86 | + * @Author: LetMeFly |
| 87 | + * @Date: 2025-07-11 23:25:31 |
| 88 | + * @LastEditors: LetMeFly.xyz |
| 89 | + * @LastEditTime: 2025-07-11 23:33:35 |
| 90 | + */ |
| 91 | +class Solution { |
| 92 | +public: |
| 93 | + int countDays(int days, vector<vector<int>>& meetings) { |
| 94 | + sort(meetings.begin(), meetings.end()); |
| 95 | + int ans = 0; |
| 96 | + int last = 0; |
| 97 | + for (vector<int> me : meetings) { |
| 98 | + // printf("last = %d, me = [%d, %d]\n", last, me[0], me[1]); |
| 99 | + if (me[0] > last + 1) { |
| 100 | + ans += me[0] - last - 1; |
| 101 | + // printf("ans += %d\n", me[0] - last - 1); |
| 102 | + } |
| 103 | + last = max(last, me[1]); |
| 104 | + } |
| 105 | + ans += days - last; |
| 106 | + return ans; |
| 107 | + } |
| 108 | +}; |
| 109 | +``` |
| 110 | +
|
| 111 | +#### Python |
| 112 | +
|
| 113 | +```python |
| 114 | +''' |
| 115 | +Author: LetMeFly |
| 116 | +Date: 2025-07-11 23:25:31 |
| 117 | +LastEditors: LetMeFly.xyz |
| 118 | +LastEditTime: 2025-07-12 12:00:22 |
| 119 | +''' |
| 120 | +from typing import List |
| 121 | +
|
| 122 | +class Solution: |
| 123 | + def countDays(self, days: int, meetings: List[List[int]]) -> int: |
| 124 | + ans = last = 0 |
| 125 | + meetings.sort() |
| 126 | + for l, r in meetings: |
| 127 | + if l > last + 1: |
| 128 | + ans += l - last - 1 |
| 129 | + last = max(last, r) |
| 130 | + ans += days - last |
| 131 | + return ans |
| 132 | +``` |
| 133 | + |
| 134 | +#### Java |
| 135 | + |
| 136 | +```java |
| 137 | +/* |
| 138 | + * @Author: LetMeFly |
| 139 | + * @Date: 2025-07-11 23:25:31 |
| 140 | + * @LastEditors: LetMeFly.xyz |
| 141 | + * @LastEditTime: 2025-07-12 16:58:44 |
| 142 | + */ |
| 143 | +import java.util.Arrays; |
| 144 | + |
| 145 | +class Solution { |
| 146 | + public int countDays(int days, int[][] meetings) { |
| 147 | + int ans = 0; |
| 148 | + int last = 0; |
| 149 | + Arrays.sort(meetings, (a, b) -> a[0] - b[0]); |
| 150 | + for (int[] me : meetings) { |
| 151 | + if (me[0] > last + 1) { |
| 152 | + ans += me[0] - last - 1; |
| 153 | + } |
| 154 | + last = Math.max(last, me[1]); |
| 155 | + } |
| 156 | + ans += days - last; |
| 157 | + return ans; |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +#### Go |
| 163 | + |
| 164 | +```go |
| 165 | +/* |
| 166 | + * @Author: LetMeFly |
| 167 | + * @Date: 2025-07-11 23:25:31 |
| 168 | + * @LastEditors: LetMeFly.xyz |
| 169 | + * @LastEditTime: 2025-07-12 17:00:47 |
| 170 | + */ |
| 171 | +package main |
| 172 | + |
| 173 | +import "slices" |
| 174 | + |
| 175 | +func countDays(days int, meetings [][]int) (ans int) { |
| 176 | + last := 0 |
| 177 | + slices.SortFunc(meetings, func(a, b []int) int {return a[0] - b[0]}) |
| 178 | + for _, me := range meetings { |
| 179 | + if me[0] > last + 1 { |
| 180 | + ans += me[0] - last - 1 |
| 181 | + } |
| 182 | + last = max(last, me[1]) |
| 183 | + } |
| 184 | + ans += days - last |
| 185 | + return |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/149301503)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/07/11/LeetCode%203169.%E6%97%A0%E9%9C%80%E5%BC%80%E4%BC%9A%E7%9A%84%E5%B7%A5%E4%BD%9C%E6%97%A5/)哦~ |
| 190 | +> |
| 191 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
0 commit comments