Sliding window, which I got to pretty quickly.
Use a sliding window to find the longest contiguous subarray with at most n 'w's. Expand the right pointer, count workdays, and shrink from the left when the count exceeds n, tracking the maximum window length. Discuss the function signature, complexity, and edge cases to show thoroughness.
Pro tip: Clarify that 'n' represents the maximum number of workdays you can flip, so the window can contain at most n 'w's. Mention that the same approach works if 'n' is larger than the total workdays (return whole array) or if the array is empty (return 0).
Specify input parameters (char array and integer n) and return type (integer max vacation length). For example, in Java: int maxVacation(char[] days, int n).
Maintain a window [left, right] and a count of workdays within it. Expand right, increment workday count if days[right] == 'w'. When count > n, shrink from left until count <= n. Update max length at each step.
Use a concrete example like ['w','h','w','w','h'] and n=1 to illustrate how the window expands and contracts, and how the maximum length is found.
Time complexity is O(m) where m is the array length, since each element is visited at most twice. Space complexity is O(1) as only a few variables are used.
Discuss: all workdays (answer = n if n < m, else m), all holidays (answer = m), n >= total workdays (answer = m), empty input (answer = 0), and n = 0 (longest contiguous holidays).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
This one tripped me up more than I'd like to admit.
Use binary search to find the next distinct value, starting from the first element. Repeatedly binary search for the first index where the value is greater than the current value, and count each distinct value found. This runs in O(k log n) time, where k is the number of distinct values, which is faster than O(n) when k log n < n.
Pro tip: Clarify that the problem guarantees the array is sorted and k is tiny; if the array were unsorted, hashing would be O(n) and optimal. Also, mention that if k is close to n, a linear scan is better, so the approach is adaptive.
Restate that the array is sorted non-decreasing and the number of distinct values k is very small compared to n. The goal is to count distinct values faster than O(n).
Explain that you can use binary search to jump to the next distinct value. Start at index 0, count it, then binary search for the first index where the value is greater than the current value. Repeat until the end of the array.
Each binary search takes O(log n) time, and we perform k binary searches, so total time is O(k log n). Space is O(1). Compare to O(n) linear scan: this is faster when k log n < n, i.e., when k is small relative to n/log n.
Mention that if k is large (close to n), linear scan is better. Also handle edge cases: empty array, all elements same, etc. Note that the approach relies on sorted order; if unsorted, hashing would be O(n) and optimal.
Summarize that the binary search approach beats linear scan when the number of distinct values is small enough that k log n < n. For example, if k is O(1), it's O(log n), much faster than O(n).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.