给定两个元素数量相等的数组 A 和 B。任务是在数组 B 中找到一个窗口的最大可能总和,使得 A[] 中同一窗口的元素是唯一的。
例子:
Input : A = [0, 1, 2, 3, 0, 1, 4]
B = [9, 8, 1, 2, 3, 4, 5]
Output : sum = 20
The maximum sum possible in B[] such that
all corresponding elements in A[] are unique
is (9+8+1+2) = 20.
Input : A = [0, 1, 2, 0, 2]
B = [5, 6, 7, 8, 2]
Output :sum = 21
一个简单的解决方案是考虑 B[] 的所有子数组。对于每个子数组,检查 A[] 中相同子数组的元素是否不同。如果不同,则将 sum 与结果进行比较并更新结果。
此解决方案的时间复杂度为 O(n 2 )
一个有效的解决方案是使用散列。
- 创建一个空的哈希表。
- 遍历数组元素。对每个元素 A[i] 执行以下操作。
- 当哈希表中存在 A[i] 时,继续从当前窗口的开头删除元素,并继续从当前总和中减去 B[] 的窗口开头元素。
- 将 B[i] 添加到当前总和并在当前总和变得更多时更新结果。
- 返回结果。
下面是上述步骤的实现。
C++
// C++ program to find the maximum
// possible sum of a window in one
// array such that elements in same
// window of other array are unique.
#include
using namespace std;
// Function to return maximum sum of window
// in B[] according to given constraints.
int returnMaxSum(int A[], int B[], int n)
{
// Map is used to store elements
// and their counts.
unordered_set mp;
int result = 0; // Initialize result
// calculating the maximum possible
// sum for each subarray containing
// unique elements.
int curr_sum = 0, curr_begin = 0;
for (int i = 0; i < n; ++i) {
// Remove all duplicate
// instances of A[i] in
// current window.
while (mp.find(A[i]) != mp.end()) {
mp.erase(A[curr_begin]);
curr_sum -= B[curr_begin];
curr_begin++;
}
// Add current instance of A[i]
// to map and to current sum.
mp.insert(A[i]);
curr_sum += B[i];
// Update result if current
// sum is more.
result = max(result, curr_sum);
}
return result;
}
// Driver code
int main()
{
int A[] = { 0, 1, 2, 3, 0, 1, 4 };
int B[] = { 9, 8, 1, 2, 3, 4, 5 };
int n = sizeof(A)/sizeof(A[0]);
cout << returnMaxSum(A, B, n);
return 0;
}
Java
// Java program to find the maximum
// possible sum of a window in one
// array such that elements in same
// window of other array are unique.
import java.util.HashSet;
import java.util.Set;
public class MaxPossibleSuminWindow
{
// Function to return maximum sum of window
// in A[] according to given constraints.
static int returnMaxSum(int A[], int B[], int n)
{
// Map is used to store elements
// and their counts.
Set mp = new HashSet();
int result = 0; // Initialize result
// calculating the maximum possible
// sum for each subarray containing
// unique elements.
int curr_sum = 0, curr_begin = 0;
for (int i = 0; i < n; ++i)
{
// Remove all duplicate
// instances of A[i] in
// current window.
while (mp.contains(A[i]))
{
mp.remove(A[curr_begin]);
curr_sum -= B[curr_begin];
curr_begin++;
}
// Add current instance of A[i]
// to map and to current sum.
mp.add(A[i]);
curr_sum += B[i];
// Update result if current
// sum is more.
result = Integer.max(result, curr_sum);
}
return result;
}
//Driver Code to test above method
public static void main(String[] args)
{
int A[] = { 0, 1, 2, 3, 0, 1, 4 };
int B[] = { 9, 8, 1, 2, 3, 4, 5 };
int n = A.length;
System.out.println(returnMaxSum(A, B, n));
}
}
// This code is contributed by Sumit Ghosh
Python3
# Python3 program to find the maximum
# possible sum of a window in one
# array such that elements in same
# window of other array are unique.
# Function to return maximum sum of window
# in B[] according to given constraints.
def returnMaxSum(A, B, n):
# Map is used to store elements
# and their counts.
mp = set()
result = 0 # Initialize result
# calculating the maximum possible
# sum for each subarray containing
# unique elements.
curr_sum = curr_begin = 0
for i in range(0, n):
# Remove all duplicate instances
# of A[i] in current window.
while A[i] in mp:
mp.remove(A[curr_begin])
curr_sum -= B[curr_begin]
curr_begin += 1
# Add current instance of A[i]
# to map and to current sum.
mp.add(A[i])
curr_sum += B[i]
# Update result if current
# sum is more.
result = max(result, curr_sum)
return result
# Driver code
if __name__ == "__main__":
A = [0, 1, 2, 3, 0, 1, 4]
B = [9, 8, 1, 2, 3, 4, 5]
n = len(A)
print(returnMaxSum(A, B, n))
# This code is contributed by Rituraj Jain
C#
// C# program to find the maximum
// possible sum of a window in one
// array such that elements in same
// window of other array are unique.
using System;
using System.Collections.Generic;
public class MaxPossibleSuminWindow
{
// Function to return maximum sum of window
// in A[] according to given constraints.
static int returnMaxSum(int []A, int []B, int n)
{
// Map is used to store elements
// and their counts.
HashSet mp = new HashSet();
int result = 0; // Initialize result
// calculating the maximum possible
// sum for each subarray containing
// unique elements.
int curr_sum = 0, curr_begin = 0;
for (int i = 0; i < n; ++i)
{
// Remove all duplicate
// instances of A[i] in
// current window.
while (mp.Contains(A[i]))
{
mp.Remove(A[curr_begin]);
curr_sum -= B[curr_begin];
curr_begin++;
}
// Add current instance of A[i]
// to map and to current sum.
mp.Add(A[i]);
curr_sum += B[i];
// Update result if current
// sum is more.
result = Math.Max(result, curr_sum);
}
return result;
}
// Driver Code
public static void Main(String[] args)
{
int []A = { 0, 1, 2, 3, 0, 1, 4 };
int []B = { 9, 8, 1, 2, 3, 4, 5 };
int n = A.Length;
Console.WriteLine(returnMaxSum(A, B, n));
}
}
/* This code has been contributed
by PrinciRaj1992*/
Javascript
输出:
20
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。