Problems > Longest Zigzag Subsequence

Longest Zigzag Subsequence

Difficulty: 1500 3000 ms 128 MB

dp greedy

A zigzag sequence strictly alternates between rising and falling steps. Print the length of the longest zigzag subsequence (not necessarily contiguous) of the array.

Input: the first line has N; the second line has N integers.

Output: the maximum zigzag subsequence length.

Constraints:

  • 1 ≤ N ≤ 105
  • −109 ≤ ai ≤ 109

Sample Tests

Input 1
6
1 7 4 9 2 5
Output 1
6
Input 2
4
3 3 3 3
Output 2
1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.