Google questions

Longest Non-Decreasing Run After One Change

GoogleOnsiteMedium

You are given an array of integers nums.

Part 1: One change, longest run

You are given an array of integers nums. You may change at most one element to any integer you like (the new value need not appear in the array). Return the length of the longest contiguous subarray of nums that is non-decreasing after making the best possible change, or no change at all.

A subarray of one element is non-decreasing, so the answer is at least 1.

  1. Example 1
    nums
    [1,0,3,4,5,2,3]
    Output
    5

    Why: Change the 0 to 1, 2 or 3; then [1, 2, 3, 4, 5] is non-decreasing with length 5. No single change also brings in the trailing 2, 3.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Longest Non-Decreasing Run After One Change: Google interview question | LoopCoach