非负整数的最长子数组
给定一个数组,返回非负整数的最长子数组的长度
例子 :
Input : {2, 3, 4, -1, -2, 1, 5, 6, 3}
Output : 4
The subarray [ 1, 5, 6, 3] has length 4 and
contains no negative integers
Input : {1, 0, 0, 1, -1, -1, 0, 0, 1, 0}
Output : 4
Subarrays [1, 0, 0, 1] and [0, 0, 1, 0] have
equal lengths but sum of first one is greater
so that will be the output.
我们遵循简单的两个指针窗口方法。最初 si 指向 curr 子数组的开始,即一个非负整数。ei 用于遍历数组。每当出现负元素时,我们都会与迄今为止的最大子数组的长度进行比较,如果 curr 长度更大,则更新开始和大小。最后,我们返回从长度 size 开始的子数组,第一个元素作为子数组的大小。
C++
// C++ program to find length of the longest
// subarray with non-negative numbers.
#include
using namespace std;
// Function that returns the longest
// subarray of non-negative integers
int longestSubarry(int *arr, int n)
{
// Initialize result
int res = 0;
// Traverse array
for (int i = 0; i < n; i++)
{
// Count of current
// non-negative integers
int curr_count = 0;
while (i < n && arr[i] >= 0)
{
curr_count++;
i++;
}
// Update result if required.
res = max(res, curr_count);
}
return res;
}
// Driver code
int main()
{
int arr[] = {1, 0, 4, 0, 1, -1, -1,
0, 0, 1, 0};
int n = sizeof(arr) / sizeof(arr[0]);
cout << longestSubarry(arr, n);
return 0;
}
Java
// Java program to find length of the longest
// subarray with non-negative numbers.
class GFG
{
// Function that returns the longest
// subarray of non-negative integers
static int longestSubarry(int arr[], int n)
{
// Initialize result
int res = 0;
// Traverse array
for (int i = 0; i < n; i++)
{
// Count of current non-
// negative integers
int curr_count = 0;
while (i < n && arr[i] >= 0)
{
curr_count++;
i++;
}
// Update result if required.
res = Math.max(res, curr_count);
}
return res;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {1, 0, 4, 0, 1, -1,
-1, 0, 0, 1, 0};
int n = arr.length;
System.out.println(longestSubarry(arr, n));
}
}
// This code is contributed by prerna saini.
Python3
# Python program to find
# length of the longest
# subarray with
# non-negative numbers.
# Function that returns
# the longest subarray
# of non-negative integers
def longestSubarry(arr,n):
# Initialize result
res = 0
# Traverse array
for i in range(n):
# Count of current
# non-negative integers
curr_count = 0
while (i < n and arr[i] >= 0):
curr_count+=1
i+=1
# Update result if required.
res = max(res, curr_count)
return res
# Driver code
arr= [1, 0, 4, 0, 1, -1, -1, 0, 0, 1, 0]
n = len(arr)
print(longestSubarry(arr, n))
# This code is contributed
# by Anant Agarwal.
C#
// C# program to find length of the longest
// subarray with non-negative numbers.
using System;
class GFG
{
// Function that returns the longest
// subarray of non-negative integers
static int longestSubarry(int []arr, int n)
{
// Initialize result
int res = 0;
// Traverse array
for (int i = 0; i < n; i++)
{
// Count of current non-
// negative integers
int curr_count = 0;
while (i < n && arr[i] >= 0)
{
curr_count++;
i++;
}
// Update result if required.
res = Math.Max(res, curr_count);
}
return res;
}
// Driver code
public static void Main()
{
int []arr = {1, 0, 4, 0, 1, -1,
-1, 0, 0, 1, 0};
int n = arr.Length;
Console.Write(longestSubarry(arr, n));
}
}
// This code is contributed by nitin mittal
PHP
= 0)
{
$curr_count++;
$i++;
}
// Update result if required.
$res = max($res, $curr_count);
}
return $res;
}
// Driver code
$arr = array(1, 0, 4, 0, 1, -1,
-1, 0, 0, 1, 0);
$n = sizeof($arr) / sizeof($arr[0]);
echo longestSubarry($arr, $n);
// This code is contributed by nitin mittal.
?>
Javascript
输出 :
5