最长交替递增递减子数组的长度
给定一个数组arr[],任务是找出最长交替子数组的长度。
A subarray {x1, x2, .. xn} is an alternating increasing decreasing sequence if its elements satisfy one of the following relations :
x1 < x2 > x3 < x4 > x5 < …. xn or
x1 > x2 < x3 > x4 < x5 > …. xn
例子:
Input: arr = {1, 2, 3, 2, 5, 6}
Output: 4
Explanation: The first elements form index 1 to 4 inclusive are alternating subarray – {2, 3, 2, 5}
Input: arr = {5, 2, 1}
Output: 2
朴素方法:朴素方法是检查每个子数组是否从每个索引开始的子数组是交替的。这个想法是使用两个嵌套的for循环。
下面是上述方法的实现
C++14
// C++ implementation of the above approach
#include
using namespace std;
int maxAlternatingSubarray(int n, int* arr)
{
// Initialize mx to store maximum
// length alternating subarrays
int i, j, max = 1, count = 1, a, b;
// Initialize a variable to check is the
// subarray starts with greater
// or smaller value
bool check;
// If length of the array is 1
// then return 1
if (n == 1)
return 1;
// Traverse for every element
for (i = 0; i < n; i++) {
// Reintialize count to 1 as
// at least one element is counted
count = 1;
// Subarray starting at every element
for (j = i; j < n - 1; j++) {
// Initialize a and b for storing
// adjacent positions
a = j;
b = j + 1;
// Check if the alternating subarray
// starts with greater
// or smaller element
if (j == i) {
// Smaller element starts first
if (arr[a] < arr[b])
check = 1;
// Greater element starts first
else if (arr[a] > arr[b])
check = 0;
}
// If check is 1 then the next
// element should be greater
if (check && arr[a] < arr[b]) {
// Increment count
count++;
}
// If check is 0 then the next
// element should be greater
else if (!check && arr[a] > arr[b]) {
// Increment count
count++;
}
else
break;
// Update the value of check
check = !check;
}
// Update the maximum value of count
if (max < count)
max = count;
}
// Return the maximum length
return max;
}
// Driver code
int main()
{
// Initialize the array
int arr[] = { 1, 2, 3, 2, 5, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
// Call thefunction and print the answer
cout << maxAlternatingSubarray(n, arr);
}
Java
// Java code for the above approach
import java.io.*;
class GFG {
static int maxAlternatingSubarray(int n, int[] arr)
{
// Initialize mx to store maximum
// length alternating subarrays
int i, j, max = 1, count = 1, a, b;
// Initialize a variable to check is the
// subarray starts with greater
// or smaller value
boolean check=false;
// If length of the array is 1
// then return 1
if (n == 1)
return 1;
// Traverse for every element
for (i = 0; i < n; i++) {
// Reintialize count to 1 as
// at least one element is counted
count = 1;
// Subarray starting at every element
for (j = i; j < n - 1; j++) {
// Initialize a and b for storing
// adjacent positions
a = j;
b = j + 1;
// Check if the alternating subarray
// starts with greater
// or smaller element
if (j == i) {
// Smaller element starts first
if (arr[a] < arr[b])
check = true;
// Greater element starts first
else if (arr[a] > arr[b])
check = false;
}
// If check is 1 then the next
// element should be greater
if (check==true && arr[a] < arr[b]) {
// Increment count
count++;
}
// If check is 0 then the next
// element should be greater
else if (check==false && arr[a] > arr[b]) {
// Increment count
count++;
}
else
break;
// Update the value of check
check = !check;
}
// Update the maximum value of count
if (max < count)
max = count;
}
// Return the maximum length
return max;
}
// Driver code
public static void main (String[] args)
{
// Initialize the array
int arr[] = { 1, 2, 3, 2, 5, 7 };
int n = arr.length;
// Call thefunction and print the answer
System.out.println( maxAlternatingSubarray(n, arr));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python implementation of the above approach
def maxAlternatingSubarray(n, arr):
# Initialize mx to store maximum
# length alternating subarrays
max = 1
count = 1
a = 0
b = 0
# Initialize a variable to check is the
# subarray starts with greater
# or smaller value
check = 0
# If length of the array is 1
# then return 1
if (n == 1):
return 1;
# Traverse for every element
for i in range(n):
# Reintialize count to 1 as
# at least one element is counted
count = 1;
# Subarray starting at every element
for j in range(i, n - 1):
# Initialize a and b for storing
# adjacent positions
a = j;
b = j + 1;
# Check if the alternating subarray
# starts with greater
# or smaller element
if (j == i):
# Smaller element starts first
if (arr[a] < arr[b]):
check = 1;
# Greater element starts first
elif (arr[a] > arr[b]):
check = 0;
# If check is 1 then the next
# element should be greater
if (check and arr[a] < arr[b]):
# Increment count
count += 1
# If check is 0 then the next
# element should be greater
elif (not check and arr[a] > arr[b]):
# Increment count
count += 1
else:
break;
# Update the value of check
check = not check;
# Update the maximum value of count
if (max < count):
max = count;
# Return the maximum length
return max;
# Driver code
# Initialize the array
arr = [ 1, 2, 3, 2, 5, 7 ];
n = len(arr);
# Call thefunction and print the answer
print(maxAlternatingSubarray(n, arr));
# This code is contributed by gfgking.
C#
// C# code for the above approach
using System;
class GFG {
static int maxAlternatingSubarray(int n, int []arr)
{
// Initialize mx to store maximum
// length alternating subarrays
int i, j, max = 1, count = 1, a, b;
// Initialize a variable to check is the
// subarray starts with greater
// or smaller value
bool check=false;
// If length of the array is 1
// then return 1
if (n == 1)
return 1;
// Traverse for every element
for (i = 0; i < n; i++) {
// Reintialize count to 1 as
// at least one element is counted
count = 1;
// Subarray starting at every element
for (j = i; j < n - 1; j++) {
// Initialize a and b for storing
// adjacent positions
a = j;
b = j + 1;
// Check if the alternating subarray
// starts with greater
// or smaller element
if (j == i) {
// Smaller element starts first
if (arr[a] < arr[b])
check = true;
// Greater element starts first
else if (arr[a] > arr[b])
check = false;
}
// If check is 1 then the next
// element should be greater
if (check==true && arr[a] < arr[b]) {
// Increment count
count++;
}
// If check is 0 then the next
// element should be greater
else if (check==false && arr[a] > arr[b]) {
// Increment count
count++;
}
else
break;
// Update the value of check
check = !check;
}
// Update the maximum value of count
if (max < count)
max = count;
}
// Return the maximum length
return max;
}
// Driver code
public static void Main ()
{
// Initialize the array
int []arr = { 1, 2, 3, 2, 5, 7 };
int n = arr.Length;
// Call thefunction and print the answer
Console.Write(maxAlternatingSubarray(n, arr));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
C++14
// C++ implementation for the above approach
#include
using namespace std;
// Function to calculate maximum length
// of alternating subarray
int longestAltSubarray(int N, int* arr)
{
// If length of the array is 1
// then return 1
if (N == 1)
return 1;
int maxLen = 0;
int currGreater = 0, currSmaller = 0;
// Traverse the temp array
for (int i = 1; i < N; i++) {
// If current element is greater
// than the previous element
if (arr[i] > arr[i - 1]) {
// Increment currGreater by 1
currGreater++;
// Reset currSmaller to 0
currSmaller = 0;
}
// If current element is smaller
// than the previous element
else if (arr[i] < arr[i - 1]) {
// Increment currSmaller by 1
currSmaller++;
// Reset currGreater to 0
currGreater = 0;
}
// If current element is equal
// to previous element
else {
// Reset both to 0
currGreater = 0;
currSmaller = 0;
}
// Swap currGreater and currSmaller
// for the next iteration
int temp = currGreater;
currGreater = currSmaller;
currSmaller = temp;
// Update maxLen
maxLen = max(maxLen,
max(currGreater, currSmaller));
}
// Return the maximum length
return maxLen + 1;
}
// Drivers code
int main()
{
// Initialize the array
int arr[] = { 1, 2, 3, 2, 5, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
// Call the function and print the answer
cout << longestAltSubarray(n, arr);
}
Java
// Java implementation for the above approach
import java.util.*;
public class GFG
{
// Function to calculate maximum length
// of alternating subarray
static int longestAltSubarray(int N, int []arr)
{
// If length of the array is 1
// then return 1
if (N == 1)
return 1;
int maxLen = 0;
int currGreater = 0, currSmaller = 0;
// Traverse the temp array
for (int i = 1; i < N; i++) {
// If current element is greater
// than the previous element
if (arr[i] > arr[i - 1]) {
// Increment currGreater by 1
currGreater++;
// Reset currSmaller to 0
currSmaller = 0;
}
// If current element is smaller
// than the previous element
else if (arr[i] < arr[i - 1]) {
// Increment currSmaller by 1
currSmaller++;
// Reset currGreater to 0
currGreater = 0;
}
// If current element is equal
// to previous element
else {
// Reset both to 0
currGreater = 0;
currSmaller = 0;
}
// Swap currGreater and currSmaller
// for the next iteration
int temp = currGreater;
currGreater = currSmaller;
currSmaller = temp;
// Update maxLen
maxLen = Math.max(maxLen,
Math.max(currGreater, currSmaller));
}
// Return the maximum length
return maxLen + 1;
}
// Drivers code
public static void main(String args[])
{
// Initialize the array
int []arr = { 1, 2, 3, 2, 5, 7 };
int n = arr.length;
// Call the function and print the answer
System.out.println(longestAltSubarray(n, arr));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python3 implementation for the above approach
from builtins import range
# Function to calculate maximum length
# of alternating subarray
def longestAltSubarray(N, arr):
# If length of the array is 1
# then return 1
if (N == 1):
return 1
maxLen = 0
currGreater = 0
currSmaller = 0
# Traverse the temp array
for i in range(1, N, 1):
# If current element is greater
# than the previous element
if (arr[i] > arr[i - 1]):
# Increment currGreater by 1
currGreater += 1
# Reset currSmaller to 0
currSmaller = 0
# If current element is smaller
# than the previous element
elif (arr[i] < arr[i - 1]):
# Increment currSmaller by 1
currSmaller += 1
# Reset currGreater to 0
currGreater = 0
# If current element is equal
# to previous element
else:
# Reset both to 0
currGreater = 0
currSmaller = 0
# Swap currGreater and currSmaller
# for the next iteration
temp = currGreater
currGreater = currSmaller
currSmaller = temp
# Update maxLen
maxLen = max(maxLen, max(currGreater,
currSmaller))
# Return the maximum length
return maxLen + 1
# Driver code
if __name__ == '__main__':
# Initialize the array
arr = [ 1, 2, 3, 2, 5, 7 ]
n = len(arr)
# Call the function and print the answer
print(longestAltSubarray(n, arr))
# This code is contributed by shikhasingrajput
C#
// C# implementation for the above approach
using System;
class GFG
{
// Function to calculate maximum length
// of alternating subarray
static int longestAltSubarray(int N, int []arr)
{
// If length of the array is 1
// then return 1
if (N == 1)
return 1;
int maxLen = 0;
int currGreater = 0, currSmaller = 0;
// Traverse the temp array
for (int i = 1; i < N; i++) {
// If current element is greater
// than the previous element
if (arr[i] > arr[i - 1]) {
// Increment currGreater by 1
currGreater++;
// Reset currSmaller to 0
currSmaller = 0;
}
// If current element is smaller
// than the previous element
else if (arr[i] < arr[i - 1]) {
// Increment currSmaller by 1
currSmaller++;
// Reset currGreater to 0
currGreater = 0;
}
// If current element is equal
// to previous element
else {
// Reset both to 0
currGreater = 0;
currSmaller = 0;
}
// Swap currGreater and currSmaller
// for the next iteration
int temp = currGreater;
currGreater = currSmaller;
currSmaller = temp;
// Update maxLen
maxLen = Math.Max(maxLen,
Math.Max(currGreater, currSmaller));
}
// Return the maximum length
return maxLen + 1;
}
// Drivers code
public static void Main()
{
// Initialize the array
int []arr = { 1, 2, 3, 2, 5, 7 };
int n = arr.Length;
// Call the function and print the answer
Console.Write(longestAltSubarray(n, arr));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出:
4
时间复杂度: O(N^2)
辅助空间: O(1)
方法:可以通过迭代数组和预先计算连续元素之间的差异来优化给定的问题。请按照以下步骤解决问题:
- 从索引 1 到末尾以及每次迭代时迭代数组:
- 如果当前元素大于前一个元素,则将currGreater增加 1 并将currSmaller重置为 0
- 否则,如果当前元素小于前一个元素,则将currSmaller增加 1 并将currGreater重置为 0
- 否则,如果当前元素和先前元素相等,则将currGreater和currSmaller都重置为 0
- 交换currGreater和currSmaller的值
- 通过将其与currGreater和currSmaller进行比较来更新最大长度的值
- 返回计算出的最大长度值
C++14
// C++ implementation for the above approach
#include
using namespace std;
// Function to calculate maximum length
// of alternating subarray
int longestAltSubarray(int N, int* arr)
{
// If length of the array is 1
// then return 1
if (N == 1)
return 1;
int maxLen = 0;
int currGreater = 0, currSmaller = 0;
// Traverse the temp array
for (int i = 1; i < N; i++) {
// If current element is greater
// than the previous element
if (arr[i] > arr[i - 1]) {
// Increment currGreater by 1
currGreater++;
// Reset currSmaller to 0
currSmaller = 0;
}
// If current element is smaller
// than the previous element
else if (arr[i] < arr[i - 1]) {
// Increment currSmaller by 1
currSmaller++;
// Reset currGreater to 0
currGreater = 0;
}
// If current element is equal
// to previous element
else {
// Reset both to 0
currGreater = 0;
currSmaller = 0;
}
// Swap currGreater and currSmaller
// for the next iteration
int temp = currGreater;
currGreater = currSmaller;
currSmaller = temp;
// Update maxLen
maxLen = max(maxLen,
max(currGreater, currSmaller));
}
// Return the maximum length
return maxLen + 1;
}
// Drivers code
int main()
{
// Initialize the array
int arr[] = { 1, 2, 3, 2, 5, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
// Call the function and print the answer
cout << longestAltSubarray(n, arr);
}
Java
// Java implementation for the above approach
import java.util.*;
public class GFG
{
// Function to calculate maximum length
// of alternating subarray
static int longestAltSubarray(int N, int []arr)
{
// If length of the array is 1
// then return 1
if (N == 1)
return 1;
int maxLen = 0;
int currGreater = 0, currSmaller = 0;
// Traverse the temp array
for (int i = 1; i < N; i++) {
// If current element is greater
// than the previous element
if (arr[i] > arr[i - 1]) {
// Increment currGreater by 1
currGreater++;
// Reset currSmaller to 0
currSmaller = 0;
}
// If current element is smaller
// than the previous element
else if (arr[i] < arr[i - 1]) {
// Increment currSmaller by 1
currSmaller++;
// Reset currGreater to 0
currGreater = 0;
}
// If current element is equal
// to previous element
else {
// Reset both to 0
currGreater = 0;
currSmaller = 0;
}
// Swap currGreater and currSmaller
// for the next iteration
int temp = currGreater;
currGreater = currSmaller;
currSmaller = temp;
// Update maxLen
maxLen = Math.max(maxLen,
Math.max(currGreater, currSmaller));
}
// Return the maximum length
return maxLen + 1;
}
// Drivers code
public static void main(String args[])
{
// Initialize the array
int []arr = { 1, 2, 3, 2, 5, 7 };
int n = arr.length;
// Call the function and print the answer
System.out.println(longestAltSubarray(n, arr));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python3 implementation for the above approach
from builtins import range
# Function to calculate maximum length
# of alternating subarray
def longestAltSubarray(N, arr):
# If length of the array is 1
# then return 1
if (N == 1):
return 1
maxLen = 0
currGreater = 0
currSmaller = 0
# Traverse the temp array
for i in range(1, N, 1):
# If current element is greater
# than the previous element
if (arr[i] > arr[i - 1]):
# Increment currGreater by 1
currGreater += 1
# Reset currSmaller to 0
currSmaller = 0
# If current element is smaller
# than the previous element
elif (arr[i] < arr[i - 1]):
# Increment currSmaller by 1
currSmaller += 1
# Reset currGreater to 0
currGreater = 0
# If current element is equal
# to previous element
else:
# Reset both to 0
currGreater = 0
currSmaller = 0
# Swap currGreater and currSmaller
# for the next iteration
temp = currGreater
currGreater = currSmaller
currSmaller = temp
# Update maxLen
maxLen = max(maxLen, max(currGreater,
currSmaller))
# Return the maximum length
return maxLen + 1
# Driver code
if __name__ == '__main__':
# Initialize the array
arr = [ 1, 2, 3, 2, 5, 7 ]
n = len(arr)
# Call the function and print the answer
print(longestAltSubarray(n, arr))
# This code is contributed by shikhasingrajput
C#
// C# implementation for the above approach
using System;
class GFG
{
// Function to calculate maximum length
// of alternating subarray
static int longestAltSubarray(int N, int []arr)
{
// If length of the array is 1
// then return 1
if (N == 1)
return 1;
int maxLen = 0;
int currGreater = 0, currSmaller = 0;
// Traverse the temp array
for (int i = 1; i < N; i++) {
// If current element is greater
// than the previous element
if (arr[i] > arr[i - 1]) {
// Increment currGreater by 1
currGreater++;
// Reset currSmaller to 0
currSmaller = 0;
}
// If current element is smaller
// than the previous element
else if (arr[i] < arr[i - 1]) {
// Increment currSmaller by 1
currSmaller++;
// Reset currGreater to 0
currGreater = 0;
}
// If current element is equal
// to previous element
else {
// Reset both to 0
currGreater = 0;
currSmaller = 0;
}
// Swap currGreater and currSmaller
// for the next iteration
int temp = currGreater;
currGreater = currSmaller;
currSmaller = temp;
// Update maxLen
maxLen = Math.Max(maxLen,
Math.Max(currGreater, currSmaller));
}
// Return the maximum length
return maxLen + 1;
}
// Drivers code
public static void Main()
{
// Initialize the array
int []arr = { 1, 2, 3, 2, 5, 7 };
int n = arr.Length;
// Call the function and print the answer
Console.Write(longestAltSubarray(n, arr));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出:
4
时间复杂度: O(N)
辅助空间: O(N)