如果一个子数组中的任意两个连续数具有相反的符号(即其中一个应该是负数,而另一个应该是正数),则该子数组被称为交替。
给定一个由 n 个整数组成的数组。对于每个索引 i,我们需要找到从 i 开始的最长交替子数组的长度。
例子:
Input : a[] = {1, -5, 1, -5}
Output : For index 0, {1, -5, 1, -5} = 4
index 1, {-5, 1, -5} = 3
index 2, {1, -5} = 2
index 3, {-5} = 1.
Input :a[] = {-5, -1, -1, 2, -2, -3}
Output : index 0 = 1,
index 1 = 1,
index 2 = 3,
index 3 = 2,
index 4 = 1,
index 5 = 1,
一种朴素的方法是使用两个循环,其中我们从每个索引 i(0 到 n-1)开始遍历整个数组并计算交替子数组的长度。
时间复杂度:O(n 2 )。
有效的方法:
请注意,当a[i]和a[i+1]符号相反时, count[i]将比count[i+1] 大 1 。否则,当它们具有相同的符号时, count[i]将为 1。
我们在这里使用动态规划。
C++
// CPP program to find longest alternating
// subarray starting from every index.
#include
using namespace std;
void longestAlternating(int arr[], int n)
{
int count[n];
// Fill count[] from end.
count[n - 1] = 1;
for (int i = n - 2; i >= 0; i--) {
if (arr[i] * arr[i + 1] < 0)
count[i] = count[i + 1] + 1;
else
count[i] = 1;
}
// Print result
for (int i = 0; i < n; i++)
cout << count[i] << " ";
}
// Driver code
int main()
{
int a[] = { -5, -1, -1, 2, -2, -3 };
int n = sizeof(a) / sizeof(a[0]);
longestAlternating(a, n);
return 0;
}
Java
// Java program to find longest alternating
// subarray starting from every index.
import java.util.*;
class Longest{
public static void longestAlternating(int arr[],
int n)
{
int[] count = new int[n];
// Fill count[] from end.
count[n - 1] = 1;
for (int i = n - 2; i >= 0; i--) {
if (arr[i] * arr[i + 1] < 0)
count[i] = count[i + 1] + 1;
else
count[i] = 1;
}
// Print result
for (int i = 0; i < n; i++)
System.out.print(count[i] + " ");
}
// driver program
public static void main(String[] args)
{
int a[] = { -5, -1, -1, 2, -2, -3 };
int n = 6;
longestAlternating(a, n);
}
}
// This code is contributed by rishabh_jain
Python3
# Python3 program to find longest alternating
# subarray starting from every index.
def longestAlternating(arr, n) :
count = [None] * n
# Fill count[] from end.
count[n - 1] = 1
i = n - 2
while i >= 0 :
if (arr[i] * arr[i + 1] < 0) :
count[i] = count[i + 1] + 1
else :
count[i] = 1;
i = i - 1
i = 0
# Print result
while i < n :
print (count[i], end = " ")
i = i + 1
# Driver Code
a = [ -5, -1, -1, 2, -2, -3 ]
n = len(a)
longestAlternating(a, n);
# This code is contributed by rishabh_jain
C#
//C# program to find longest alternating
// subarray starting from every index.
using System;
class Longest
{
public static void longestAlternating(int []arr,
int n)
{
int[] count = new int[n];
// Fill count[] from end.
count[n - 1] = 1;
for (int i = n - 2; i >= 0; i--)
{
if (arr[i] * arr[i + 1] < 0)
count[i] = count[i + 1] + 1;
else
count[i] = 1;
}
// Print result
for (int i = 0; i < n; i++)
Console.Write(count[i] + " ");
}
// Driver program
public static void Main()
{
int []a = { -5, -1, -1, 2, -2, -3 };
int n = 6;
longestAlternating(a, n);
}
}
// This code is contributed by vt_m
PHP
= 0; $i--)
{
if ($arr[$i] * $arr[$i + 1] < 0)
$count[$i] = $count[$i + 1] + 1;
else
$count[$i] = 1;
}
// Print result
for ( $i = 0; $i < $n; $i++)
echo $count[$i] , " ";
}
// Driver code
$a = array( -5, -1, -1, 2, -2, -3 );
$n =count($a);
longestAlternating($a, $n);
// This code is contributed by anuj_67.
?>
Javascript
输出:
1 1 3 2 1 1
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。