给定一个由n个数字组成的数组,其中每个数字与上一个数字之间的差不超过1。找到最长的连续子数组,以使该范围内的最大数与最小数之差不超过一。
例子:
Input : {3, 3, 4, 4, 5, 6}
Output : 4
The longest subarray here is {3, 3, 4, 4}
Input : {7, 7, 7}
Output : 3
The longest subarray here is {7, 7, 7}
Input : {9, 8, 8, 9, 9, 10}
Output : 5
The longest subarray here is {9, 8, 8, 9, 9}
如果序列中最大值和最小值之间的差不超过1,则该序列仅包含一个或两个连续的数字。这个想法是遍历数组,并跟踪当前子数组中的当前元素和上一个元素。如果找到与当前或先前不同的元素,则更新当前和先前。如果需要,我们也会更新结果。
C++
// C++ code to find longest
// subarray with difference
// between max and min as at-most 1.
#include
using namespace std;
int longestSubarray(int input[],
int length)
{
int prev = -1;
int current, next;
int prevCount = 0, currentCount = 1;
// longest constant range length
int longest = 1;
// first number
current = input[0];
for (int i = 1; i < length; i++)
{
next = input[i];
// If we see same number
if (next == current)
{
currentCount++;
}
// If we see different number,
// but same as previous.
else if (next == prev)
{
prevCount += currentCount;
prev = current;
current = next;
currentCount = 1;
}
// If number is neither same
// as previous nor as current.
else
{
longest = max(longest,
currentCount + prevCount);
prev = current;
prevCount = currentCount;
current = next;
currentCount = 1;
}
}
return max(longest,
currentCount + prevCount);
}
// Driver Code
int main()
{
int input[] = { 5, 5, 6, 7, 6 };
int n = sizeof(input) / sizeof(int);
cout << longestSubarray(input, n);
return 0;
}
// This code is contributed
// by Arnab Kundu
Java
// Java code to find longest subarray with difference
// between max and min as at-most 1.
public class Demo {
public static int longestSubarray(int[] input)
{
int prev = -1;
int current, next;
int prevCount = 0, currentCount = 1;
// longest constant range length
int longest = 1;
// first number
current = input[0];
for (int i = 1; i < input.length; i++) {
next = input[i];
// If we see same number
if (next == current) {
currentCount++;
}
// If we see different number, but
// same as previous.
else if (next == prev) {
prevCount += currentCount;
prev = current;
current = next;
currentCount = 1;
}
// If number is neither same as previous
// nor as current.
else {
longest = Math.max(longest, currentCount + prevCount);
prev = current;
prevCount = currentCount;
current = next;
currentCount = 1;
}
}
return Math.max(longest, currentCount + prevCount);
}
public static void main(String[] args)
{
int[] input = { 5, 5, 6, 7, 6 };
System.out.println(longestSubarray(input));
}
}
Python 3
# Python 3 code to find longest
# subarray with difference
# between max and min as at-most 1.
def longestSubarray(input, length):
prev = -1
prevCount = 0
currentCount = 1
# longest constant range length
longest = 1
# first number
current = input[0]
for i in range(1, length):
next = input[i]
# If we see same number
if next == current :
currentCount += 1
# If we see different number,
# but same as previous.
elif next == prev :
prevCount += currentCount
prev = current
current = next
currentCount = 1
# If number is neither same
# as previous nor as current.
else:
longest = max(longest,
currentCount +
prevCount)
prev = current
prevCount = currentCount
current = next
currentCount = 1
return max(longest,
currentCount + prevCount)
# Driver Code
if __name__ == "__main__":
input = [ 5, 5, 6, 7, 6 ]
n = len(input)
print(longestSubarray(input, n))
# This code is contributed
# by ChitraNayal
C#
// C# code to find longest
// subarray with difference
// between max and min as
// at-most 1.
using System;
class GFG
{
public static int longestSubarray(int[] input)
{
int prev = -1;
int current, next;
int prevCount = 0,
currentCount = 1;
// longest constant
// range length
int longest = 1;
// first number
current = input[0];
for (int i = 1;
i < input.Length; i++)
{
next = input[i];
// If we see same number
if (next == current)
{
currentCount++;
}
// If we see different number,
// but same as previous.
else if (next == prev)
{
prevCount += currentCount;
prev = current;
current = next;
currentCount = 1;
}
// If number is neither
// same as previous
// nor as current.
else
{
longest = Math.Max(longest,
currentCount +
prevCount);
prev = current;
prevCount = currentCount;
current = next;
currentCount = 1;
}
}
return Math.Max(longest,
currentCount + prevCount);
}
// Driver Code
public static void Main(String[] args)
{
int[] input = {5, 5, 6, 7, 6};
Console.WriteLine(longestSubarray(input));
}
}
// This code is contributed
// by Kirti_Mangal
PHP
输出:
3
时间复杂度: O(n),其中n是输入数组的长度。