A monotonic subsequence is a sequence of numbers \$a_1, a_2, ..., a_n\$ such that
$$a_1 \le a_2 \le ... \le a_n \\ \text{or} \\ a_1 \ge a_2 \ge ... \ge a_n$$
[1, 3, 3, 7, 9, 13, 13, 100]
is a monotonic (non-decreasing) subsequence, as well as [9, 4, 4, 3, 0, -10, -12]
(this one is non-increasing), but [1, 3, 6, 9, 8]
is not. Given a list of integers (in any reasonable format), output the smallest number N
such that the sequence of these integers can be split into N
monotonic sequences.
Examples
[1, 3, 7, 5, 4, 2] -> [[1, 3, 7], [5, 4, 2]] -> 2
[1, 2, 3, 4, 5, 6] -> [1, 2, 3, 4, 5, 6] -> 1
[3, 1, 5, 5, 6] -> [[3, 1], [5, 5, 6]] -> 2
[4, 6, 8, 9, 1, 6] -> [[4, 6, 8, 9], [1, 6]] -> 2
[3, 3, 3, 3] -> [[3, 3, 3, 3]] -> 1
[7] -> [[7]] -> 1
[] -> [] -> anything (you don't actually have to handle an empty list case)
[1, 3, 2, -1, 6, 9, 10, 2, 1, -12] -> [[1, 3], [2, -1], [6, 9, 10], [2, 1, -12]] -> 4
[4,4,8,8,1,4,5] -> 2
\$\endgroup\$0 / undefined
, it sounds like it should be either 0 or the representation ofundefined
in our language, but from your comment on Jonathan Allan's Jelly answer, it looks likeundefined
meansanything
... Which one is it? In the second case, I would suggest writinganything
instead ofundefined
\$\endgroup\$