对于每个 Array 元素,最长的子数组的长度在左边紧邻较小的元素
给定一个长度为N的数组arr[] ,任务是为给定数组中的每个元素找到最左边具有较小元素的最长子数组的长度。
例子:
Input: arr[] = { 2, 1, 7, 6, 7 }
Output: 0 0 2 0 1
Explanation:
Index 0 (2): No substring is present to the immediate left having all smaller elements. So, length=0
Index 1 (1): No substring is present to the immediate left having all smaller elements. So, length=0
Index 2 (7): Substring {2, 1} is present to the immediate left having all smaller elements. So, length=2
Index 3 (6): No substring is present to the immediate left having all smaller elements. So, length=0
Index 4 (2): Substring {6} is present to the immediate left having all smaller elements. So, length=1
Input: arr[] = { 4, 5, 7, 6, 10 }
Output: 0 1 2 0 4
方法:对于每个元素都向左移动,直到左边的元素更大或数组结束,找到最左边的元素较小的最长子数组的长度。请按照以下步骤解决此问题:
- 遍历数组arr[]中的每个元素。
- 对于每个元素,在左侧运行另一个循环。
- 计算所有小于当前元素的元素,直到出现更大的元素或数组结束。
- 根据以上观察打印答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the length of the longest
// subarray with smaller elements on the immediate
// left for each element in the given array
void subarrayLength(int* arr, int n)
{
for (int i = 0; i < n; i++) {
int ans = 0;
for (int j = i - 1; j >= 0; j--) {
// If a greater element comes
if (arr[i] <= arr[j]) {
break;
}
// Else
ans++;
}
cout << ans << " ";
}
}
// Driver Code
int main()
{
int n = 5;
int arr[n] = { 1, 4, 2, 6, 3 };
subarrayLength(arr, n);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find the length of the longest
// subarray with smaller elements on the immediate
// left for each element in the given array
static void subarrayLength(int arr[], int n)
{
for(int i = 0; i < n; i++)
{
int ans = 0;
for(int j = i - 1; j >= 0; j--)
{
// If a greater element comes
if (arr[i] <= arr[j])
{
break;
}
// Else
ans++;
}
System.out.print(ans + " ");
}
}
// Driver Code
public static void main(String args[])
{
int n = 5;
int arr[] = { 1, 4, 2, 6, 3 };
subarrayLength(arr, n);
}
}
// This code is contributed by gfgking
Python3
# Python program for the above approach
# Function to find the length of the longest
# subarray with smaller elements on the immediate
# left for each element in the given array
def subarrayLength(arr, n):
for i in range(0, n):
ans = 0
for j in range(i-1, -1, -1):
# If a greater element comes
if (arr[i] <= arr[j]):
break
# Else
ans += 1
print(ans, end=" ")
# Driver Code
if __name__ == "__main__":
n = 5
arr = [1, 4, 2, 6, 3]
subarrayLength(arr, n)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the length of the longest
// subarray with smaller elements on the immediate
// left for each element in the given array
static void subarrayLength(int []arr, int n)
{
for(int i = 0; i < n; i++)
{
int ans = 0;
for(int j = i - 1; j >= 0; j--)
{
// If a greater element comes
if (arr[i] <= arr[j])
{
break;
}
// Else
ans++;
}
Console.Write(ans + " ");
}
}
// Driver Code
public static void Main()
{
int n = 5;
int []arr = { 1, 4, 2, 6, 3 };
subarrayLength(arr, n);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
0 1 0 3 0
时间复杂度: O(N 2 )
辅助空间: O(1)