Trains the technique from
LeetCode 3738Longest Non-Decreasing Subarray After Replacing at Most One ElementThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A cable crew surveys the pylons of a ski lift from the bottom station upward. Pylon i sits at height[i], measured in centimetres against a survey datum, so a pylon below the datum has a negative height.
A run of consecutive pylons is settled when the heights never drop as you walk it upward: for every neighbouring pair in the run, the later pylon is at least as high as the earlier one. Two pylons at exactly the same height are fine.
Before the survey is signed off, the crew may regrade at most one pylon: pick a single index and change height[i] to any integer you like, positive or negative, with no restriction to the heights already recorded. Every other pylon keeps its height.
Return the greatest number of pylons in a settled run of consecutive pylons that the crew can end up with. Regrading is optional, so a line that is already settled from end to end needs no change.
Example 1
Regrading pylon 1 to -2 leaves the heights -3, -2, -1, 6, 6, which never drop, so the whole line of 5 pylons is settled.
Example 2
Regrading pylon 1 to -5 leaves the heights -5, -5, -4, 7, -6, 2. Pylons 0 through 3 read -5, -5, -4, 7 with no drop, a settled run of 4 pylons.
Example 3
The recorded heights -7, -7, 0, 5 never drop as they stand, so all 4 pylons are already one settled run and the crew regrades nothing.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def longest_settled_stretch(height: list[int]) -> int:public int longestSettledStretch(int[] height)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.