← 返回 meta 的题目列表Longest Vacation With PTO (flip W to H)
类型:online_judge
Problem
You are given a character array days of length n representing a calendar year, where:
'H' = Holiday
'W' = Workday
You also have PTO personal time-off days. You may use PTO on a 'W' day (costing 1 PTO) to treat it as a vacation day.
Return the maximum length of a consecutive vacation you can take, i.e., the length of the longest contiguous subarray in which the number of 'W' days is at most PTO.
Example:
days = [W, H, H, W, W, H, W], PTO = 2 -> 5
Input/Output
Input: char array/string days containing only H and W, integer PTO
Output: an integer, the maximum consecutive vacation length
Constraints
1 <= n <= 2 * 10^5
0 <= PTO <= n
days[i] ∈ {'H','W'}
Test Cases
days = WHHWWHW, PTO = 2 -> 5
days = HHHH, PTO = 1 -> 4
days = WWWW, PTO = 2 -> 2
days = WHWHWH, PTO = 0 -> 1
days = WHW, PTO = 3 -> 3
Example
Input
WHHWWHW
2
Output
5