pyassistant

Longest alternating-sign subsequence with non-decreasing absolute values

Given a list of integers nums, find the length of the longest subsequence (not necessarily contiguous) that satisfies both of the following: 1) consecutive elements in the subsequence alternate sign (positive vs negative); 2) the absolute values are non-decreasing along the subsequence (each next absolute value is >= the previous). Zeros are ignored (cannot be part of the subsequence). Return the length of such longest subsequence.

Example:

Input:
([1, -2, 3, -4, 5])
Output:
5

Make sure you return your solution, don't print!

AI

Bot

Trying to solve my challenge? Ask if you must, or press the purple button so I can analyze your code.