We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d85050f commit b784332Copy full SHA for b784332
Codes/2765-longest-alternating-subarray.py
@@ -0,0 +1,26 @@
1
+'''
2
+Author: LetMeFly
3
+Date: 2024-01-23 22:22:14
4
+LastEditors: LetMeFly
5
+LastEditTime: 2024-01-23 22:25:36
6
7
+from typing import List
8
+
9
+class Solution:
10
+ def get1(self, oddLoc=1) -> int:
11
+ evenLoc = -oddLoc
12
+ ans = 1
13
+ cnt = 1
14
+ for i in range(len(self.nums)):
15
+ shouldAdd = oddLoc if i % 2 else evenLoc
16
+ if 1 + 1 == len(self.nums) or self.nums[i + 1] != self.nums[i] + shouldAdd or (cnt == 1 and shouldAdd == -1):
17
+ ans = max(ans, cnt)
18
19
+ else:
20
+ cnt += 1
21
+ return ans
22
23
+ def alternatingSubarray(self, nums: List[int]) -> int:
24
+ self.nums = nums
25
+ ans = max(self.get1(), self.get1(-1))
26
+ return ans if ans >= 2 else -1
0 commit comments