Rank Selection Plus Longest Leave-Assisted Break
Heaps, Sorting & SearchAuto-graded · 19 test casesLast updated
Combines selecting the element at a 0-indexed rank in descending order with a sliding window finding the longest stretch containing at most a given number of workdays.
Write one function that answers two unrelated questions and returns both answers in a list.
Part A: Value at a given rank
You get a list of integers values and a 0-indexed rank rank. Imagine the list sorted from largest to smallest. Report the element at position rank in that ordering.
rank = 0is the largest value.rank = 1is the second largest value, and so on.- Duplicates count separately. Each copy occupies its own position in the sorted order, so this is not about distinct values.
Part B: Longest break using leave days
You get a list days describing a year. Each entry is one of two strings:
"H": a holiday, already free."W": a workday.
You also get leave, the number of workdays you may convert into days off by taking leave. A day counts as part of a break if it is a holiday, or if it is a workday you chose to cover with leave. You may cover at most leave workdays.
Find the largest possible length of a run of consecutive days in which every day is off. If days is empty, the answer is 0.
Output
Return a list of two integers: [value_at_rank, longest_break].
Constraints
0 <= rank < len(values)(sovaluesis non-empty).valuescontains integers, possibly negative or repeated.dayscontains only the strings"H"and"W"; it may be empty.leave >= 0.- The two parts use independent inputs and do not influence each other.
Examples
Example 1
Input: rank_and_longest_break([5, -3, 9, 1], 0, ['W', 'H', 'H', 'W', 'W', 'H', 'W'], 2)
Output: [9, 5]
Example 2
Input: rank_and_longest_break([5, -3, 9, 1], 1, ['W', 'H', 'H', 'W', 'W', 'H', 'W'], 2)
Output: [5, 5]
Example 3
Input: rank_and_longest_break([5, -3, 9, 1], 3, ['W', 'H', 'H', 'W', 'W', 'H', 'W'], 2)
Output: [-3, 5]
Hints
Hint 1
For Part A, a min-heap that keeps only the rank + 1 largest values seen so far has the answer at its root. Watch the off-by-one from 0-indexing.
Hint 2
For Part B, reframe the question: what is the longest contiguous segment that contains at most `leave` workdays?
Hint 3
Use two pointers. Expand the right end, count workdays in the window, and move the left end forward while the count exceeds `leave`; record the window length after each step.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The two parts are independent, so each is solved on its own and the results are combined.
Part A: value at a descending rank
Maintain a min-heap holding the rank + 1 largest values seen so far. Until the heap has that many items, push every value. After that, a new value only matters if it is larger than the heap's root (the smallest of the current top group); in that case replace the root with it. When the scan ends, the root is the smallest of the rank + 1 largest values, which is exactly the element at index rank in descending order.
Why it is correct: the heap always holds the largest min(rank + 1, prefix length) values of the prefix as a multiset. A value not exceeding the root cannot belong to the top group, because at least rank + 1 values already match or beat it. A larger value displaces the current smallest of the group, preserving the invariant. Duplicates occupy separate slots, matching the sorted-array definition.
Complexity: time O(n log rank), space O(rank).
Part B: longest break
A break is valid exactly when the chosen segment contains at most leave workdays, since every workday inside can be covered and holidays are free. So the task is the longest contiguous window with at most leave "W" entries.
Use a sliding window: extend the right end, counting workdays. While the count exceeds leave, advance the left end, decrementing the count whenever a workday leaves. After each step, record the window length.
Why it is correct: every recorded window is valid (at most leave workdays). For any optimal window [L, R], when the right end reaches R the left end never passes L, because the shrinking stops as soon as the window is valid and [L, R] is valid, so the recorded length is at least R - L + 1.
Complexity: time O(m) since each index enters and leaves the window at most once, space O(1).
Overall: O(n log rank + m) time.
Reference solution
import heapq
from typing import List
def rank_and_longest_break(values: List[int], rank: int, days: List[str], leave: int) -> List[int]:
# Part A: min-heap of the (rank + 1) largest values seen so far.
heap = []
for v in values:
if len(heap) < rank + 1:
heapq.heappush(heap, v)
elif v > heap[0]:
heapq.heapreplace(heap, v)
value_at_rank = heap[0]
# Part B: longest window with at most `leave` workdays.
left = 0
workdays = 0
best = 0
for right, d in enumerate(days):
if d == 'W':
workdays += 1
while workdays > leave:
if days[left] == 'W':
workdays -= 1
left += 1
best = max(best, right - left + 1)
return [value_at_rank, best]