One Direction Sequence
Sometimes you need to check whether a sequence keeps moving in one direction. Given a list nums, return True if it is entirely non-decreasing or entirely non-increasing. Equal neighboring elements do not break monotonicity. If the direction changes from increasing to decreasing or back, return False.
Примеры
Ввод
[1, 2, 2, 3]Вывод
TrueВвод
[6, 5, 4, 4]Вывод
TrueВвод
[1, 3, 2]Вывод
FalseНужно реализовать
def is_monotonic(nums: list[int]) -> bool:
Теги
array
Přihlaste se, abyste viděli historii odevzdání
Přihlásit sePřihlaste se, abyste mohli použít AI mentora
Přihlásit seVisible tests
1
Input
[1,2,2,3]Expected
True2
Input
[6,5,4,4]Expected
True3
Input
[1,3,2]Expected
False4
Input
[1,2,3,4,5,6,5]Expected
False5
Input
[-3,-2,-1]Expected
True6
Input
[-5,-6,-7]Expected
TrueFocus radio
Paused · SomaFM · Fluid