Longest Strictly Rising Window After One Edit
Arrays & HashingAuto-graded · 22 test casesLast updated
Find the longest contiguous strictly increasing window in an integer array after replacing at most one element with any integer, using prefix and suffix run lengths.
Longest Strictly Rising Window After One Edit
You are given a list of integers readings. You may overwrite at most one position with any integer you like. The new value can be positive, negative or zero, and it does not need to appear anywhere else in the list. You may also leave the list unchanged.
After this optional edit, look for a contiguous block of the list in which every value is strictly greater than the one before it.
Return the greatest possible length of such a block, taking the best choice of edit (or no edit).
Rules
- "Strictly greater" means equal neighbors do not count as rising.
- The replacement value must be an integer, so two neighbors can be bridged only if at least one integer lies strictly between them.
- A list with a single element yields
1. A list with no elements yields0. - The input list is only edited conceptually. You only need to return a length.
Constraints
1 <= len(readings) <= 10^5-10^9 <= readings[i] <= 10^9- The replacement value may be any integer, with no bound.
Examples
Example 1
Input: longest_rising_window_with_edit([1, 2, 8, 4, 5])
Output: 5
Example 2
Input: longest_rising_window_with_edit([5, 4, 3, 2, 1])
Output: 2
Hints
Hint 1
Without any edit, the answer is just the longest run of strictly increasing consecutive values. Think about how to record, for every index, the run length ending there and the run length starting there.
Hint 2
If you rewrite position i, you can always make it extend one neighbouring run by one: pick a huge value to extend the run on its left, or a tiny value to extend the run on its right.
Hint 3
To join the run ending at i-1 with the run starting at i+1, the new value must be an integer strictly between readings[i-1] and readings[i+1]. That is possible only when readings[i+1] - readings[i-1] >= 2.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
First compute two helper arrays:
ends[i]: the length of the strictly increasing run that finishes at indexi.starts[i]: the length of the strictly increasing run that begins at indexi.
Each is built in one linear pass. The answer starts at max(ends), which is the best result with no edit.
Then consider rewriting each index i in turn:
- Extend leftward. If
i > 0, set the new value larger than everything before it. The window is the run ending ati-1plus positioni, so its length isends[i-1] + 1. - Extend rightward. If
i < n-1, set the new value smaller than everything after it. The length isstarts[i+1] + 1. - Bridge both sides. If both neighbors exist and an integer fits strictly between them, that is
readings[i-1] + 1 < readings[i+1], then the run ending ati-1, the edited element and the run starting ati+1join. The length isends[i-1] + 1 + starts[i+1].
The result is the maximum over all these candidates.
Why it is correct
Take an optimal window [l, r] that becomes strictly increasing after editing at most one index i inside it.
- If no edit is needed, the window is one existing run, so it is counted by
max(ends). - If
iis at the left end of the window, the rest[i+1, r]is untouched and increasing, so its length is at moststarts[i+1]. Case 2 achievesstarts[i+1] + 1. - If
iis at the right end, symmetric reasoning gives case 1. - If
iis strictly inside, the parts[l, i-1]and[i+1, r]are untouched and increasing. The new value must be an integer strictly betweenreadings[i-1]andreadings[i+1]. That exists exactly when the bridge condition holds, and the length is then at mostends[i-1] + 1 + starts[i+1].
Each candidate is also actually achievable, because the replacement value is unbounded, so "very large" and "very small" always exist. The candidates therefore match the true optimum.
Edge cases follow naturally. A single element returns 1. A fully increasing list returns n. A strictly decreasing list with n >= 2 returns 2. Equal neighbors or a gap of exactly 1 between the two sides of a bridge make the bridge fail, so only the extension candidates apply.
Complexity
- Time: O(n), with three linear passes.
- Space: O(n) for the two helper arrays.
Reference solution
from typing import List
def longest_rising_window_with_edit(readings: List[int]) -> int:
n = len(readings)
if n <= 1:
return n
# ends[i]: length of the strictly increasing run ending at i
ends = [1] * n
for i in range(1, n):
if readings[i - 1] < readings[i]:
ends[i] = ends[i - 1] + 1
# starts[i]: length of the strictly increasing run starting at i
starts = [1] * n
for i in range(n - 2, -1, -1):
if readings[i] < readings[i + 1]:
starts[i] = starts[i + 1] + 1
best = max(ends)
for i in range(n):
current = 1
if i > 0:
current = max(current, ends[i - 1] + 1)
if i + 1 < n:
current = max(current, starts[i + 1] + 1)
if i > 0 and i + 1 < n and readings[i - 1] + 1 < readings[i + 1]:
current = ends[i - 1] + 1 + starts[i + 1]
best = max(best, current)
return best