Maximum Vacation Length and Efficient Distinct Value Counting
Arrays & HashingAuto-graded · 20 test casesLast updated
Tests sliding window technique for contiguous subarrays with constraints and binary search optimization for counting distinct values in sorted arrays.
Write a function that solves two independent subproblems and returns both answers as a list [answer_a, answer_b].
Problem A: Maximum Vacation Length
You have a calendar represented as an array where each element is either 'w' (workday) or 'h' (holiday). You can use up to budget paid time-off days to turn workdays into days off.
Find the maximum number of consecutive days off you can create.
When you use PTO on a workday, it becomes a day off for the purpose of building a consecutive streak. Holidays are already days off and don't require PTO.
Problem B: Counting Distinct Values Efficiently
You are given a sorted array (non-decreasing order) that contains many duplicate values. The number of distinct values is much smaller than the array length.
Count how many distinct values exist in the array using an approach faster than visiting every element when duplicates are common.
Parameters
calendar: List of strings, each either'w'or'h'budget: Non-negative integer, the number of PTO days availablesorted_arr: List of integers in non-decreasing order
Return Value
Return [max_vacation_days, distinct_count] where:
max_vacation_days: Maximum length of consecutive days off achievabledistinct_count: Number of distinct values insorted_arr
Constraints
0 ≤ len(calendar) ≤ 100,0000 ≤ budget ≤ len(calendar)0 ≤ len(sorted_arr) ≤ 1,000,000sorted_arris sorted in non-decreasing order-10^9 ≤ sorted_arr[i] ≤ 10^9- If
calendaris empty, the maximum vacation length is0 - If
sorted_arris empty, the distinct count is0
Examples
Example 1
Input: vacation_and_distinct_count(['w', 'h', 'h', 'w', 'h', 'w'], 2, [1, 1, 1, 2, 2, 5, 5, 5, 9])
Output: [5, 4]
Example 2
Input: vacation_and_distinct_count(['w', 'w', 'h', 'w'], 1, [-3, -3, -3, -1, 0, 0, 4])
Output: [2, 4]
Example 3
Input: vacation_and_distinct_count(['h', 'h', 'h'], 0, [-2, -2, 7, 7])
Output: [3, 2]
Example 4
Input: vacation_and_distinct_count([], 3, [])
Output: [0, 0]
Hints
Hint 1
For Problem A: Think about maintaining a window where you track how many workdays are inside it. What happens when the window has too many workdays?
Hint 2
For Problem B: In a sorted array, all occurrences of the same value form one continuous segment. Can you jump to the end of each segment without examining every element?
Hint 3
For Problem A: A two-pointer sliding window approach can solve this in one pass through the calendar array.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem combines two independent algorithmic challenges that test different optimization techniques.
Problem A: Maximum Vacation with PTO Budget
The core insight is to reframe this as finding the longest contiguous subarray containing at most budget workdays. We use a sliding window technique with two pointers:
- Expand the right pointer to include more days
- Count workdays (
'w') as they enter the window - When workdays exceed the budget, shrink from the left until the window is valid again
- Track the maximum window size seen
Why this works: The window property (having at most budget workdays) is monotonic. If a window becomes invalid, shrinking from the left is the only way to fix it. The algorithm never skips a potentially optimal window because each right boundary is explored with the smallest valid left boundary.
Time Complexity: O(m) where m is the calendar length. Although there's a nested loop, each position enters and leaves the window at most once, giving amortized linear time.
Space Complexity: O(1), using only a few variables.
Problem B: Efficient Distinct Value Counting
In a sorted array, all copies of any value form one contiguous block. Instead of examining each element, we can jump to the end of each duplicate run using binary search:
- Count the current value as distinct
- Use
bisect_rightto find where the current value ends (the first position with a strictly larger value) - Jump directly to that position and repeat
Why this works: bisect_right finds the first index where the value changes. By searching only in the suffix starting after the current position, we efficiently skip over all duplicates. Each distinct value is processed exactly once.
Time Complexity: O(k log n) where k is the number of distinct values and n is the array length. This beats O(n) linear scanning when k << n / log n, which is typical when duplicates are common.
Space Complexity: O(1), using only a few variables.
Edge Cases Handled:
- Empty inputs return 0 for their respective answers
- All holidays: returns the full calendar length
- All workdays: returns min(length, budget)
- Budget exceeds workday count: returns full calendar length
- All identical values in sorted array: one binary search returns 1
- All distinct values: degrades gracefully but still correct
Solution Code
from bisect import bisect_right
from typing import List
def vacation_and_distinct_count(calendar: List[str], budget: int, sorted_arr: List[int]) -> List[int]:
# Problem A: Find longest window with at most 'budget' workdays
left = 0
workdays_in_window = 0
max_days_off = 0
for right in range(len(calendar)):
if calendar[right] == 'w':
workdays_in_window += 1
while workdays_in_window > budget:
if calendar[left] == 'w':
workdays_in_window -= 1
left += 1
max_days_off = max(max_days_off, right - left + 1)
# Problem B: Count distinct values using binary search to skip duplicates
distinct_count = 0
pos = 0
while pos < len(sorted_arr):
distinct_count += 1
pos = bisect_right(sorted_arr, sorted_arr[pos], pos + 1)
return [max_days_off, distinct_count]