返回介绍

solution / 2700-2799 / 2765.Longest Alternating Subarray / README_EN

发布于 2024-06-17 01:03:00 字数 5120 浏览 0 评论 0 收藏 0

2765. Longest Alternating Subarray

中文文档

Description

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

Solutions

Solution 1: Enumeration

We can enumerate the left endpoint $i$ of the subarray, and for each $i$, we need to find the longest subarray that satisfies the condition. We can start traversing to the right from $i$, and each time we encounter adjacent elements whose difference does not satisfy the alternating condition, we find a subarray that satisfies the condition. We can use a variable $k$ to record whether the difference of the current element should be $1$ or $-1$. If the difference of the current element should be $-k$, then we take the opposite of $k$. When we find a subarray $nums[i..j]$ that satisfies the condition, we update the answer to $\max(ans, j - i + 1)$.

The time complexity is $O(n^2)$, where $n$ is the length of the array. We need to enumerate the left endpoint $i$ of the subarray, and for each $i$, we need $O(n)$ time to find the longest subarray that satisfies the condition. The space complexity is $O(1)$.

class Solution:
  def alternatingSubarray(self, nums: List[int]) -> int:
    ans, n = -1, len(nums)
    for i in range(n):
      k = 1
      j = i
      while j + 1 < n and nums[j + 1] - nums[j] == k:
        j += 1
        k *= -1
      if j - i + 1 > 1:
        ans = max(ans, j - i + 1)
    return ans
class Solution {
  public int alternatingSubarray(int[] nums) {
    int ans = -1, n = nums.length;
    for (int i = 0; i < n; ++i) {
      int k = 1;
      int j = i;
      for (; j + 1 < n && nums[j + 1] - nums[j] == k; ++j) {
        k *= -1;
      }
      if (j - i + 1 > 1) {
        ans = Math.max(ans, j - i + 1);
      }
    }
    return ans;
  }
}
class Solution {
public:
  int alternatingSubarray(vector<int>& nums) {
    int ans = -1, n = nums.size();
    for (int i = 0; i < n; ++i) {
      int k = 1;
      int j = i;
      for (; j + 1 < n && nums[j + 1] - nums[j] == k; ++j) {
        k *= -1;
      }
      if (j - i + 1 > 1) {
        ans = max(ans, j - i + 1);
      }
    }
    return ans;
  }
};
func alternatingSubarray(nums []int) int {
  ans, n := -1, len(nums)
  for i := range nums {
    k := 1
    j := i
    for ; j+1 < n && nums[j+1]-nums[j] == k; j++ {
      k *= -1
    }
    if t := j - i + 1; t > 1 && ans < t {
      ans = t
    }
  }
  return ans
}
function alternatingSubarray(nums: number[]): number {
  let ans = -1;
  const n = nums.length;
  for (let i = 0; i < n; ++i) {
    let k = 1;
    let j = i;
    for (; j + 1 < n && nums[j + 1] - nums[j] === k; ++j) {
      k *= -1;
    }
    if (j - i + 1 > 1) {
      ans = Math.max(ans, j - i + 1);
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文