Python cpp單迴圈速解Leetcode 3105 Longest Strictly Increasing or Strictly Decreasing Subarray
用單迴圈就可解,甚至也沒有用到if, switch, tenary
[Py3解請進]
------------
It can be solved with a single loop, without even using if, switch, tenary
class Solution:
def longestMonotonicSubarray(self, nums: List[int]) -> int:
n, ans, inc, dec=len(nums), 1, 1, 1
for i in range(1, n):
A=nums[i]>nums[i-1]
B=nums[i]<nums[i-1]
inc=A*inc+1
dec=B*dec+1
ans=max(ans, dec, inc)
return ans
沒有留言:
張貼留言