Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a positive integer days representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array meetings of size n where, meetings[i] = [start_i, end_i] represents the starting and ending days of meeting i (inclusive).
Return the count of days when the employee is available for work but no meetings are scheduled.
Note: The meetings may overlap.
Example 1:
Input: days = 10, meetings = [[5,7],[1,3],[9,10]]
Output: 2
Explanation:
There is no meeting scheduled on the 4th and 8th days.
Example 2:
Input: days = 5, meetings = [[2,4],[1,3]]
Output: 1
Explanation:
There is no meeting scheduled on the 5th day.
Example 3:
Input: days = 6, meetings = [[1,6]]
Output: 0
Explanation:
Meetings are scheduled for all working days.
Constraints:
1 <= days <= 1091 <= meetings.length <= 105meetings[i].length == 21 <= meetings[i][0] <= meetings[i][1] <= daysProblem summary: You are given a positive integer days representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array meetings of size n where, meetings[i] = [start_i, end_i] represents the starting and ending days of meeting i (inclusive). Return the count of days when the employee is available for work but no meetings are scheduled. Note: The meetings may overlap.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
10 [[5,7],[1,3],[9,10]]
5 [[2,4],[1,3]]
6 [[1,6]]
merge-intervals)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3169: Count Days Without Meetings
class Solution {
public int countDays(int days, int[][] meetings) {
Arrays.sort(meetings, (a, b) -> a[0] - b[0]);
int ans = 0, last = 0;
for (var e : meetings) {
int st = e[0], ed = e[1];
if (last < st) {
ans += st - last - 1;
}
last = Math.max(last, ed);
}
ans += days - last;
return ans;
}
}
// Accepted solution for LeetCode #3169: Count Days Without Meetings
func countDays(days int, meetings [][]int) (ans int) {
sort.Slice(meetings, func(i, j int) bool { return meetings[i][0] < meetings[j][0] })
last := 0
for _, e := range meetings {
st, ed := e[0], e[1]
if last < st {
ans += st - last - 1
}
last = max(last, ed)
}
ans += days - last
return
}
# Accepted solution for LeetCode #3169: Count Days Without Meetings
class Solution:
def countDays(self, days: int, meetings: List[List[int]]) -> int:
meetings.sort()
ans = last = 0
for st, ed in meetings:
if last < st:
ans += st - last - 1
last = max(last, ed)
ans += days - last
return ans
// Accepted solution for LeetCode #3169: Count Days Without Meetings
impl Solution {
pub fn count_days(days: i32, mut meetings: Vec<Vec<i32>>) -> i32 {
meetings.sort_by_key(|m| m[0]);
let mut ans = 0;
let mut last = 0;
for e in meetings {
let st = e[0];
let ed = e[1];
if last < st {
ans += st - last - 1;
}
last = last.max(ed);
}
ans + (days - last)
}
}
// Accepted solution for LeetCode #3169: Count Days Without Meetings
function countDays(days: number, meetings: number[][]): number {
meetings.sort((a, b) => a[0] - b[0]);
let [ans, last] = [0, 0];
for (const [st, ed] of meetings) {
if (last < st) {
ans += st - last - 1;
}
last = Math.max(last, ed);
}
ans += days - last;
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.