给定一个数组,任务是计算所有元素都不同的连续子数组的长度总和。
例子:
Input : arr[] = {1, 2, 3}
Output : 10
{1, 2, 3} is a subarray of length 3 with
distinct elements. Total length of length
three = 3.
{1, 2}, {2, 3} are 2 subarray of length 2
with distinct elements. Total length of
lengths two = 2 + 2 = 4
{1}, {2}, {3} are 3 subarrays of length 1
with distinct element. Total lengths of
length one = 1 + 1 + 1 = 3
Sum of lengths = 3 + 4 + 3 = 10
Input : arr[] = {1, 2, 1}
Output : 7
Input : arr[] = {1, 2, 3, 4}
Output : 20
一个简单的解决方案是考虑所有子数组并检查每个子数组是否具有不同的元素或不使用散列。并添加具有不同元素的所有子数组的长度。如果我们使用散列来查找不同的元素,那么在假设散列搜索和插入操作需要 O(1) 时间的情况下,这种方法需要 O(n 2 ) 时间。
一个有效的解决方案是基于这样一个事实:如果我们知道子数组 arr[i..j] 中的所有元素都是不同的,那么这个子数组中不同元素子数组的所有长度的总和是 ((j-i+1)*( j-i+2))/2。如何?子数组的可能长度为 1, 2, 3,……, j – i +1。因此,总和将为 ((j – i +1)*(j – i +2))/2。
我们首先从第一个元素开始找到最大的子数组(具有不同的元素)。我们使用上面的公式计算这个子数组中的长度总和。为了找到不同元素的下一个子数组,我们增加起点 i 和结束点 j 除非 (i+1, j) 是不同的。如果不可能,那么我们再次增加 i 并以同样的方式前进。
下面是这个方法的实现:
C++
// C++ program to calculate sum of lengths of subarrays
// of distinct elements.
#include
using namespace std;
// Returns sum of lengths of all subarrays with distinct
// elements.
int sumoflength(int arr[], int n)
{
// For maintaining distinct elements.
unordered_set s;
// Initialize ending point and result
int j = 0, ans = 0;
// Fix starting point
for (int i=0; i
Java
// Java program to calculate sum of lengths of subarrays
// of distinct elements.
import java.util.*;
class geeks
{
// Returns sum of lengths of all subarrays
// with distinct elements.
public static int sumoflength(int[] arr, int n)
{
// For maintaining distinct elements.
Set s = new HashSet<>();
// Initialize ending point and result
int j = 0, ans = 0;
// Fix starting point
for (int i = 0; i < n; i++)
{
while (j < n && !s.contains(arr[j]))
{
s.add(arr[i]);
j++;
}
// Calculating and adding all possible length
// subarrays in arr[i..j]
ans += ((j - i) * (j - i + 1)) / 2;
// Remove arr[i] as we pick new stating point
// from next
s.remove(arr[i]);
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 4 };
int n = arr.length;
System.out.println(sumoflength(arr, n));
}
}
// This code is contributed by
// sanjeev2552
Python 3
# Python 3 program to calculate sum of
# lengths of subarrays of distinct elements.
# Returns sum of lengths of all subarrays
# with distinct elements.
def sumoflength(arr, n):
# For maintaining distinct elements.
s = []
# Initialize ending point and result
j = 0
ans = 0
# Fix starting point
for i in range(n):
# Find ending point for current
# subarray with distinct elements.
while (j < n and (arr[j] not in s)):
s.append(arr[j])
j += 1
# Calculating and adding all possible
# length subarrays in arr[i..j]
ans += ((j - i) * (j - i + 1)) // 2
# Remove arr[i] as we pick new
# stating point from next
s.remove(arr[i])
return ans
# Driven Code
if __name__=="__main__":
arr = [1, 2, 3, 4]
n = len(arr)
print(sumoflength(arr, n))
# This code is contributed by ita_c
C#
// C# program to calculate sum of lengths of subarrays
// of distinct elements
using System;
using System.Collections.Generic;
public class geeks
{
// Returns sum of lengths of all subarrays
// with distinct elements.
public static int sumoflength(int[] arr, int n)
{
// For maintaining distinct elements.
HashSet s = new HashSet();
// Initialize ending point and result
int j = 0, ans = 0;
// Fix starting point
for (int i = 0; i < n; i++)
{
while (j < n && !s.Contains(arr[j]))
{
s.Add(arr[i]);
j++;
}
// Calculating and adding all possible length
// subarrays in arr[i..j]
ans += ((j - i) * (j - i + 1)) / 2;
// Remove arr[i] as we pick new stating point
// from next
s.Remove(arr[i]);
}
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int[] arr = { 1, 2, 3, 4 };
int n = arr.Length;
Console.WriteLine(sumoflength(arr, n));
}
}
/* This code is contributed by PrinciRaj1992 */
Javascript
输出:
20
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。