给定一个由N个整数组成的数组arr [] ,任务是从给定数组中找到所有严格增加的子数组的最大和最小元素之间的差之和。所有子数组都必须采用最长的形式,即,如果子数组[i,j]形成严格增加的子数组,则应将其视为一个整体,而不是[i,k]和[k + 1,j]一些i <= k <= j。
A subarray is said to be strictly increasing if for every ith index in the subarray, except the last index, arr[i+1] > arr[i]
例子:
Input: arr[ ] = {7, 1, 5, 3, 6, 4}
Output: 7
Explanation:
All possible increasing subarrays are {7}, {1, 5}, {3, 6} and {4}
Therefore, sum = (7 – 7) + (5 – 1) + (6 – 3) + (4 – 4) = 7
Input: arr[ ] = {1, 2, 3, 4, 5, 2}
Output: 4
Explanation:
All possible increasing subarrays are {1, 2, 3, 4, 5} and {2}
Therefore, sum = (5 – 1) + (2 – 2) = 4
方法:
请按照以下步骤解决问题:
- 遍历数组,并为每次迭代找到当前子数组严格增加到的最右边的元素。
- 令i为当前子数组的起始元素,并为j严格增加当前子数组的索引。该子数组的最大值和最小值分别为arr [j]和arr [i] 。因此,将(arr [j] – arr [i])加到和上。
- 继续迭代第(j + 1)个索引的下一个子数组。
- 遍历数组后,打印sum的最终值。
下面是上述方法的实现:
C++
// C++ Program to find the sum of
// differences of maximum and minimum
// of strictly increasing subarrays
#include
using namespace std;
// Function to calculate and return the
// sum of differences of maximum and
// minimum of strictly increasing subarrays
int sum_of_differences(int arr[], int N)
{
// Stores the sum
int sum = 0;
int i, j, flag;
// Traverse the array
for (i = 0; i < N - 1; i++) {
if (arr[i] < arr[i + 1]) {
flag = 0;
for (j = i + 1; j < N - 1; j++) {
// If last element of the
// increasing sub-array is found
if (arr[j] >= arr[j + 1]) {
// Update sum
sum += (arr[j] - arr[i]);
i = j;
flag = 1;
break;
}
}
// If the last element of the array
// is reached
if (flag == 0 && arr[i] < arr[N - 1]) {
// Update sum
sum += (arr[N - 1] - arr[i]);
break;
}
}
}
// Return the sum
return sum;
}
// Driver Code
int main()
{
int arr[] = { 6, 1, 2, 5, 3, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << sum_of_differences(arr, N);
return 0;
}
Java
// Java program to find the sum of
// differences of maximum and minimum
// of strictly increasing subarrays
class GFG{
// Function to calculate and return the
// sum of differences of maximum and
// minimum of strictly increasing subarrays
static int sum_of_differences(int arr[], int N)
{
// Stores the sum
int sum = 0;
int i, j, flag;
// Traverse the array
for(i = 0; i < N - 1; i++)
{
if (arr[i] < arr[i + 1])
{
flag = 0;
for(j = i + 1; j < N - 1; j++)
{
// If last element of the
// increasing sub-array is found
if (arr[j] >= arr[j + 1])
{
// Update sum
sum += (arr[j] - arr[i]);
i = j;
flag = 1;
break;
}
}
// If the last element of the array
// is reached
if (flag == 0 && arr[i] < arr[N - 1])
{
// Update sum
sum += (arr[N - 1] - arr[i]);
break;
}
}
}
// Return the sum
return sum;
}
// Driver Code
public static void main (String []args)
{
int arr[] = { 6, 1, 2, 5, 3, 4 };
int N = arr.length;
System.out.print(sum_of_differences(arr, N));
}
}
// This code is contributed by chitranayal
Python3
# Python3 program to find the sum of
# differences of maximum and minimum
# of strictly increasing subarrays
# Function to calculate and return the
# sum of differences of maximum and
# minimum of strictly increasing subarrays
def sum_of_differences(arr, N):
# Stores the sum
sum = 0
# Traverse the array
i = 0
while(i < N - 1):
if arr[i] < arr[i + 1]:
flag = 0
for j in range(i + 1, N - 1):
# If last element of the
# increasing sub-array is found
if arr[j] >= arr[j + 1]:
# Update sum
sum += (arr[j] - arr[i])
i = j
flag = 1
break
# If the last element of the array
# is reached
if flag == 0 and arr[i] < arr[N - 1]:
# Update sum
sum += (arr[N - 1] - arr[i])
break
i += 1
# Return the sum
return sum
# Driver Code
arr = [ 6, 1, 2, 5, 3, 4 ]
N = len(arr)
print(sum_of_differences(arr, N))
# This code is contributed by yatinagg
C#
// C# program to find the sum of
// differences of maximum and minimum
// of strictly increasing subarrays
using System;
class GFG{
// Function to calculate and return the
// sum of differences of maximum and
// minimum of strictly increasing subarrays
static int sum_of_differences(int []arr, int N)
{
// Stores the sum
int sum = 0;
int i, j, flag;
// Traverse the array
for(i = 0; i < N - 1; i++)
{
if (arr[i] < arr[i + 1])
{
flag = 0;
for(j = i + 1; j < N - 1; j++)
{
// If last element of the
// increasing sub-array is found
if (arr[j] >= arr[j + 1])
{
// Update sum
sum += (arr[j] - arr[i]);
i = j;
flag = 1;
break;
}
}
// If the last element of the array
// is reached
if (flag == 0 && arr[i] < arr[N - 1])
{
// Update sum
sum += (arr[N - 1] - arr[i]);
break;
}
}
}
// Return the sum
return sum;
}
// Driver Code
public static void Main (string []args)
{
int []arr = { 6, 1, 2, 5, 3, 4 };
int N = arr.Length;
Console.Write(sum_of_differences(arr, N));
}
}
// This code is contributed by rock_cool
Javascript
5
时间复杂度: O(N)
辅助空间: O(1)