978. Longest Turbulent Subarray
In: arr
Out: 最长turbulent subarray
符合条件1或是条件2即是turbulent.
条件1:
arr 中奇数位置(前)的值要比偶数位置(後)的值大
arr 中偶数位置(前)的值要比奇数位置(後)的值小
Or
条件2:
arr 中偶数位置(前)的值要比奇数位置(後)的值大
arr 中奇数位置(前)的值要比偶数位置(後)的值小
A subarray is a contiguous part of array.
class Solution:
def maxTurbulenceSize(self, arr: List[int]) -> int:
c1 = 1
c2 = 1
c1_max = (-1)
c2_max = (-1)
if len(arr)==1:
return 1
status = (-1)
for i in range(len(arr)-1):
print("i-Status:",i,status)
print("c1_max:",c1_max)
print("c2_max:",c2_max)
# if is even
if i%2==0:
if arr[i]>arr[i+1]:
if status==2 or status==(-1):
c2+=1
status = 2
else:
c2+=1
c1 = 1
status = (2)
if c1 > c1_max and status==1:
c1_max = c1
if c2 > c2_max and status==2:
c2_max = c2
elif arr[i]<arr[i+1]:
if status==1 or status==(-1):
c1+=1
status = 1
else:
c1+=1
c2 = 1
status = (1)
if c1 > c1_max and status==1:
c1_max = c1
if c2 > c2_max and status==2:
c2_max = c2
else:
# end of subarry
if c1 > c1_max and status==1:
c1_max = c1
if c2 > c2_max and status==2:
c2_max = c2
c1 = 1
c2 = 1
status = (-1)
# if is odd
elif i%2!=0:
if arr[i]<arr[i+1]:
if status==2 or status==(-1):
c2+=1
status = 2
else:
c2+=1
c1 = 1
status = (2)
if c1 > c1_max and status==1:
c1_max = c1
if c2 > c2_max and status==2:
c2_max = c2
elif arr[i]>arr[i+1]:
if status==1 or status==(-1):
c1+=1
status = 1
else:
c1+=1
c2 = 1
status = (1)
if c1 > c1_max and status==1:
c1_max = c1
if c2 > c2_max and status==2:
c2_max = c2
else:
status = (-1)
if c1 > c1_max:
c1_max = c1
if c2 > c2_max:
c2_max = c2
c1 = 1
c2 = 1
if c1_max > c2_max:
print("c1")
return c1_max
elif c1_max == c2_max and c1_max==(-1):
print("the same")
return 1
else: #c2_max > c1_max or (c1_max == c2_max and c1_max!=(-1))
print("c2")
return c2_max
Medium
LeetCode 付费版在坑钱? Is LeetCode Premium Worth It in 2021?
创立一个属於自己的App,那就需要两个必须的部份,设备与知识。 自己所使用的设备为Apple Mac...
接下来讲讲删除 部分... 在查询的View那边可以看到下方程序码 @Html.ActionLink...
这是我个人的考上Cissp的分享会,其中分享如何有效的念书,提供想考Cissp的朋友一些参考。 ...
大家好,我是西瓜,你现在看到的是 2021 iThome 铁人赛『如何在网页中绘制 3D 场景?从 ...
虽然我一直知道Null是一个空物体, 可以让物体多一个中心点,但透过练习才知道, 他可以中央控管涂层...