Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active December 10, 2023 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save primaryobjects/c32e8959f0814bbdb728d023ecb52d2d to your computer and use it in GitHub Desktop.
Save primaryobjects/c32e8959f0814bbdb728d023ecb52d2d to your computer and use it in GitHub Desktop.
class Solution:
def alternatingSubarray(self, nums: List[int]) -> int:
# Combination search O(n^2).
max_length = -1
for i in range(len(nums) - 1):
cur_length = 0
is_odd = True
# Check the subarray of values from i to the end of the array.
for j in range(i + 1, len(nums)):
# Compare the current and prior array values.
s0 = nums[j-1]
s1 = nums[j]
if is_odd:
if s1 - s0 == 1:
# Valid alternating.
cur_length += 1
else:
# Not valid.
if cur_length > 1:
# Check if we've found a longest subarray up to this point.
max_length = max(cur_length + 1, max_length)
cur_length = 0
break
else:
if s1 - s0 == -1:
# Valid alternating.
cur_length += 1
else:
# Not valid.
if cur_length > 1:
# Check if we've found a longest subarray up to this point.
max_length = max(cur_length + 1, max_length)
cur_length = 0
break
is_odd = not is_odd
if cur_length:
# We've reached the end of the array with a valid alternating subarray.
max_length = max(cur_length + 1, max_length)
# If we've reached the end of the array then we have the longest possible alternating subarray and can stop here.
if j == len(nums) - 1:
break
return max_length
class Solution:
def alternatingSubarray(self, nums: List[int]) -> int:
results = []
max_length = 0
for i in range(len(nums) - 1):
result = [nums[i]]
is_odd = True
for j in range(i + 1, len(nums)):
s0 = nums[j-1]
s1 = nums[j]
if is_odd:
if s1 - s0 == 1:
# Valid alternating.
result.append(s1)
else:
# Not valid.
if len(result) > 1:
results += result
max_length = max(len(result), max_length)
result = None
break
else:
if s1 - s0 == -1:
# Valid alternating.
result.append(s1)
else:
# Not valid.
if len(result) > 1:
results += result
max_length = max(len(result), max_length)
result = None
break
is_odd = not is_odd
if result:
max_length = max(len(result), max_length)
# If we've reached the end of the array then we have the longest possible alternating subarray and can stop here.
if j == len(nums) - 1:
break
if not max_length:
max_length = -1
return max_length
2765. Longest Alternating Subarray
Solved
Easy
Topics
Companies
Hint
You are given a 0-indexed integer array nums. A subarray s of length m is called alternating if:
m is greater than 1.
s1 = s0 + 1.
The 0-indexed subarray s looks like [s0, s1, s0, s1,...,s(m-1) % 2]. In other words, s1 - s0 = 1, s2 - s1 = -1, s3 - s2 = 1, s4 - s3 = -1, and so on up to s[m - 1] - s[m - 2] = (-1)m.
Return the maximum length of all alternating subarrays present in nums or -1 if no such subarray exists.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [2,3,4,3,4]
Output: 4
Explanation: The alternating subarrays are [3,4], [3,4,3], and [3,4,3,4]. The longest of these is [3,4,3,4], which is of length 4.
Example 2:
Input: nums = [4,5,6]
Output: 2
Explanation: [4,5] and [5,6] are the only two alternating subarrays. They are both of length 2.
Constraints:
2 <= nums.length <= 100
1 <= nums[i] <= 104
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment