Ctrl K

Longest Seesaw Subsequence

Medium
Call a sequence a seesaw if the differences between neighbouring elements strictly alternate in sign: up, down, up, … or down, up, down, … The first difference may have either sign, and a zero difference is not allowed. A single element, or two different elements, also count as a seesaw. For example, [2, 9, 4, 6, 1] is a seesaw (differences +7, −5, +2, −5), while [2, 5, 8, 1] is not: its first two differences are both positive. You get an array values. You may cross out any of its elements, keeping the rest in their original order. Return the greatest length of a seesaw you can get this way. Can you do it in O(n)?
Example 1
values=[3, 8, 8, 5, 6, 2, 2, 9] → 6

For example 3, 8, 5, 6, 2, 9: the differences +5, −3, +1, −4, +7 alternate in sign, length 6. The repeated 8 and 2 have to go, because a difference of 0 is neither a rise nor a fall.

Example 2
values=[10, 20, 30, 40] → 2

The sequence only goes up, so nothing longer than two elements works, for example 10, 40.

Example 3
values=[7, 7, 7] → 1
Constraints
  • 1 ≤ len(values) ≤ 1000
  • 0 ≤ values[i] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumReach the Last Cell MediumAs Many Trades as You Like MediumFewest Meetings to Cancel MediumLongest Chain of Segments Medium
loading Python…
Input
values=[3, 8, 8, 5, 6, 2, 2, 9]
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 136 tests on the server, hidden ones included.