import operator
def max_saw(arr):
if len(arr) < 3:
return len(arr)
n, nmax = 2, 2
for i in range(2, len(arr)):
if (arr[i] > arr[i-1]) ^ (arr[i-1] > arr[i-2]):
n += 1
else:
n = 2
if n > nmax:
nmax = n
return nmax
def max_saw_op(arr):
if len(arr) < 3:
return len(arr)
n, nmax = 2, 2
ops = [operator.gt, operator.lt]
op = ops[arr[1] > arr[0]]
for i in range(2, len(arr)):
if op(arr[i], arr[i-1]):
n += 1
op = ops[1 - ops.index(op)]
else:
op = ops[arr[i] > arr[i-1]]
n = 2
if n > nmax:
nmax = n
return nmax
def max_saw_min(arr):
n, nmax = 2, 2
for i in range(2, len(arr)):
n = n + 1 if (arr[i] > arr[i-1]) ^ (arr[i-1] > arr[i-2]) else 2
nmax = max(n, nmax)
return nmax
print(max_saw([1,2,3]), end=" ")
print(max_saw([5, 7, 6, 3, 4, 2, 7, 1, 8, 9, 4, 5]), end=" ")
print(max_saw([1, -2, 3, -4, 5]))
print(max_saw_op([1,2,3]), end=" ")
print(max_saw_op([5, 7, 6, 3, 4, 2, 7, 1, 8, 9, 4, 5]), end=" ")
print(max_saw_op([1, -2, 3, -4, 5]))
print(max_saw_min([1,2,3]), end=" ")
print(max_saw_min([5, 7, 6, 3, 4, 2, 7, 1, 8, 9, 4, 5]), end=" ")
print(max_saw_min([1, -2, 3, -4, 5]))
aW1wb3J0IG9wZXJhdG9yCgpkZWYgbWF4X3NhdyhhcnIpOgoJaWYgbGVuKGFycikgPCAzOgoJCXJldHVybiBsZW4oYXJyKQoJbiwgbm1heCA9IDIsIDIKCWZvciBpIGluIHJhbmdlKDIsIGxlbihhcnIpKToKCQlpZiAoYXJyW2ldID4gYXJyW2ktMV0pIF4gKGFycltpLTFdID4gYXJyW2ktMl0pOgoJCQluICs9IDEKCQllbHNlOgoJCQluID0gMgoJCWlmIG4gPiBubWF4OgoJCQlubWF4ID0gbgoJcmV0dXJuIG5tYXgKCmRlZiBtYXhfc2F3X29wKGFycik6CglpZiBsZW4oY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