📌  相关文章
📜  满足给定条件的子数组的最大大小

📅  最后修改于: 2021-10-25 06:25:43             🧑  作者: Mango

给定一个整数数组arr[] 。任务是返回满足任一条件的最大大小子数组的长度:

  1. arr[k] > arr[k + 1]k 为奇数时arr[k] < arr[k + 1]k 为偶数时
  2. arr[k] > arr[k + 1]k 是偶数时arr[k] < arr[k + 1]k 是奇数时

例子:

方法:因为只需要相邻元素之间的比较。因此,如果比较要由-1, 0, 1表示那么我们需要最长的交替1, -1, 1, …, -1序列(从 1 或 -1 开始),其中:

  • 0 -> arr[i] = arr[i + 1]
  • 1 -> arr[i] > arr[i + 1]
  • -1 -> arr[i] < arr[i + 1]

例如,如果我们有一个数组arr[] = {9, 4, 2, 10, 7, 8, 8, 1, 9}那么比较将是{1, 1, -1, 1, -1, 0 , -1, 1}和所有可能的子数组是{1} , {1, -1, 1, -1} , {0} , {-1, 1}
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
 
// Function that compares a and b
int cmp(int a, int b)
{
    return (a > b) - (a < b);
}
 
// Function to return length of longest subarray
// that satisfies one of the given conditions
int maxSubarraySize(int arr[], int n)
{
    int ans = 1;
    int anchor = 0;
 
    for (int i = 1; i < n; i++)
    {
        int c = cmp(arr[i - 1], arr[i]);
        if (c == 0)
            anchor = i;
        else if (i == n - 1 ||
                 c * cmp(arr[i], arr[i + 1]) != -1)
        {
            ans = max(ans, i - anchor + 1);
            anchor = i;
        }
    }
         
    return ans;
}
     
 
// Driver Code
int main()
{
    int arr[] = {9, 4, 2, 10, 7, 8, 8, 1, 9};
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Print the required answer
    cout << maxSubarraySize(arr, n);
}
 
// This code is contributed by
// Surendra_Gangwar


Java
// Java implementation of the approach
 
class GFG
{
     
// Function that compares a and b
static int cmp(int a, int b)
{
    if(a > b)
        return 1;
    else if(a == b)
        return 0;
    else
        return -1;
}
 
// Function to return length of longest subarray
// that satisfies one of the given conditions
static int maxSubarraySize(int []arr, int n)
{
    int ans = 1;
    int anchor = 0;
 
    for (int i = 1; i < n; i++)
    {
        int c = cmp(arr[i - 1], arr[i]);
        if (c == 0)
            anchor = i;
        else if (i == n - 1 ||
                c * cmp(arr[i], arr[i + 1]) != -1)
        {
            ans = Math.max(ans, i - anchor + 1);
            anchor = i;
        }
    }
         
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int []arr = {9, 4, 2, 10, 7, 8, 8, 1, 9};
    int n = arr.length;
 
    // Print the required answer
    System.out.println(maxSubarraySize(arr, n));
}
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
 
# Function that compares a and b
def cmp(a, b):
    return (a > b) - (a < b)
 
# Function to return length of longest subarray
# that satisfies one of the given conditions
def maxSubarraySize(arr):
    N = len(arr)
    ans = 1
    anchor = 0
 
    for i in range(1, N):
        c = cmp(arr[i - 1], arr[i])
        if c == 0:
            anchor = i
        elif i == N - 1 or c * cmp(arr[i], arr[i + 1]) != -1:
            ans = max(ans, i - anchor + 1)
            anchor = i
    return ans
 
 
# Driver program
arr = [9, 4, 2, 10, 7, 8, 8, 1, 9]
 
# Print the required answer
print(maxSubarraySize(arr))


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function that compares a and b
static int cmp(int a, int b)
{
    if(a > b)
        return 1;
    else if(a == b)
        return 0;
    else
        return -1;
}
 
// Function to return length of longest subarray
// that satisfies one of the given conditions
static int maxSubarraySize(int []arr, int n)
{
    int ans = 1;
    int anchor = 0;
 
    for (int i = 1; i < n; i++)
    {
        int c = cmp(arr[i - 1], arr[i]);
        if (c == 0)
            anchor = i;
        else if (i == n - 1 ||
                c * cmp(arr[i], arr[i + 1]) != -1)
        {
            ans = Math.Max(ans, i - anchor + 1);
            anchor = i;
        }
    }
         
    return ans;
}
     
 
// Driver Code
static void Main()
{
    int []arr = {9, 4, 2, 10, 7, 8, 8, 1, 9};
    int n = arr.Length;
 
    // Print the required answer
    Console.WriteLine(maxSubarraySize(arr, n));
}
}
 
// This code is contributed by mits


PHP
 $b) - ($a < $b);
}
 
// Function to return length of longest subarray
// that satisfies one of the given conditions
function maxSubarraySize($arr)
{
    $N = sizeof($arr);
    $ans = 1;
    $anchor = 0;
 
    for ($i = 1; $i < $N; $i++)
    {
        $c = cmp($arr[$i - 1], $arr[$i]);
        if ($c == 0)
            $anchor = $i;
             
        else if ($i == $N - 1 or
                 $c * cmp($arr[$i],
                          $arr[$i + 1]) != -1)
        {
            $ans = max($ans, $i - $anchor + 1);
            $anchor = $i;
        }
    }    
    return $ans;
}
 
// Driver Code
$arr = array(9, 4, 2, 10, 7, 8, 8, 1, 9);
 
// Print the required answer
echo maxSubarraySize($arr);
 
// This code is contributed by Ryuga
?>


Javascript


输出:
5

时间复杂度: O(N),其中 N 是数组的长度
空间复杂度: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程