给定大小为n的数组。求出增加的子序列的最大和。
例子:
Input : arr[] = { 1, 20, 4, 2, 5 }
Output : Maximum sum of increasing subsequence is = 21
The subsequence 1, 20 gives maximum sum which is 21
Input : arr[] = { 4, 2, 3, 1, 5, 8 }
Output : Maximum sum of increasing subsequence is = 18
The subsequence 2, 3, 5, 8 gives maximum sum which is 18
先决条件
该解决方案利用了二进制索引树和映射。
动态编程方法:DP方法在O(n ^ 2)中。
解决方案
步骤1 :
第一步是将所有值插入映射中,稍后我们可以将这些数组值映射到Binary Indexed Tree的索引中。
第2步 :
迭代地图并分配索引。这将对数组{4,2,3,8,5,2}执行
2将被分配索引1
3将被分配索引2
4将被分配索引3
5将被分配索引4
8将被分配索引5
第三步:
构造二进制索引树。
第四步 :
对于给定数组中的每个值,请执行以下操作。
使用BIT找到直到该位置的最大和,然后用新的最大值更新BIT
第五步:
返回存在于二进制索引树中最后位置的最大和。
C++
// C++ code for Maximum Sum
// Increasing Subsequence
#include
using namespace std;
// Returns the maximum value of
// the increasing subsequence
// till that index
// Link to understand getSum function
// https://www.geeksforgeeks.org/binary-indexed-tree-or-fenwick-tree-2/
int getSum(int BITree[], int index)
{
int sum = 0;
while (index > 0) {
sum = max(sum, BITree[index]);
index -= index & (-index);
}
return sum;
}
// Updates a node in Binary Index
// Tree (BITree) at given index in
// BITree. The max value is updated
// by taking max of 'val' and the
// already present value in the node.
void updateBIT(int BITree[], int newIndex,
int index, int val)
{
while (index <= newIndex) {
BITree[index] = max(val, BITree[index]);
index += index & (-index);
}
}
// maxSumIS() returns the maximum
// sum of increasing subsequence
// in arr[] of size n
int maxSumIS(int arr[], int n)
{
int newindex = 0, max_sum;
map uniqueArr;
// Inserting all values in map uniqueArr
for (int i = 0; i < n; i++) {
uniqueArr[arr[i]] = 0;
}
// Assigning indexes to all
// the values present in map
for (map::iterator it = uniqueArr.begin();
it != uniqueArr.end(); it++) {
// newIndex is actually the count of
// unique values in the array.
newindex++;
uniqueArr[it->first] = newindex;
}
// Constructing the BIT
int* BITree = new int[newindex + 1];
// Initializing the BIT
for (int i = 0; i <= newindex; i++) {
BITree[i] = 0;
}
for (int i = 0; i < n; i++) {
// Finding maximum sum till this element
max_sum = getSum(BITree, uniqueArr[arr[i]] - 1);
// Updating the BIT with new maximum sum
updateBIT(BITree, newindex,
uniqueArr[arr[i]], max_sum + arr[i]);
}
// return maximum sum
return getSum(BITree, newindex);
}
// Driver program
int main()
{
int arr[] = { 1, 101, 2, 3, 100, 4, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Maximum sum is = " << maxSumIS(arr, n);
return 0;
}
Java
// JAVA code for Maximum Sum
// Increasing Subsequence
import java.util.*;
class GFG{
// Returns the maximum value of
// the increasing subsequence
// till that index
// Link to understand getSum function
// https://www.geeksforgeeks.org/
// binary-indexed-tree-or-fenwick-tree-2/
static int getSum(int BITree[], int index)
{
int sum = 0;
while (index > 0)
{
sum = Math.max(sum,
BITree[index]);
index -= index & (-index);
}
return sum;
}
// Updates a node in Binary Index
// Tree (BITree) at given index in
// BITree. The max value is updated
// by taking max of 'val' and the
// already present value in the node.
static void updateBIT(int BITree[],
int newIndex,
int index, int val)
{
while (index <= newIndex)
{
BITree[index] = Math.max(val,
BITree[index]);
index += index & (-index);
}
}
// maxSumIS() returns the maximum
// sum of increasing subsequence
// in arr[] of size n
static int maxSumIS(int arr[],
int n)
{
int newindex = 0, max_sum;
HashMap uniqueArr =
new HashMap<>();
// Inserting all values in map
// uniqueArr
for (int i = 0; i < n; i++)
{
uniqueArr.put(arr[i], 0);
}
// Assigning indexes to all
// the values present in map
for (Map.Entry entry :
uniqueArr.entrySet())
{
// newIndex is actually the
// count of unique values in
// the array.
newindex++;
uniqueArr.put(entry.getKey(),
newindex);
}
// Constructing the BIT
int []BITree = new int[newindex + 1];
// Initializing the BIT
for (int i = 0; i <= newindex; i++)
{
BITree[i] = 0;
}
for (int i = 0; i < n; i++)
{
// Finding maximum sum till
// this element
max_sum = getSum(BITree,
uniqueArr.get(arr[i]) - 3);
// Updating the BIT with
// new maximum sum
updateBIT(BITree, newindex,
uniqueArr.get(arr[i]),
max_sum + arr[i]);
}
// return maximum sum
return getSum(BITree,
newindex);
}
// Driver program
public static void main(String[] args)
{
int arr[] = {1, 101, 2,
3, 100, 4, 5};
int n = arr.length;
System.out.print("Maximum sum is = " +
maxSumIS(arr, n));
}
}
// This code is contributed by shikhasingrajput
C#
// C# code for Maximum Sum
// Increasing Subsequence
using System;
using System.Collections.Generic;
class GFG{
// Returns the maximum value of
// the increasing subsequence
// till that index
// Link to understand getSum function
// https://www.geeksforgeeks.org/
// binary-indexed-tree-or-fenwick-tree-2/
static int getSum(int []BITree,
int index)
{
int sum = 0;
while (index > 0)
{
sum = Math.Max(sum,
BITree[index]);
index -= index & (-index);
}
return sum;
}
// Updates a node in Binary Index
// Tree (BITree) at given index in
// BITree. The max value is updated
// by taking max of 'val' and the
// already present value in the node.
static void updateBIT(int []BITree,
int newIndex,
int index, int val)
{
while (index <= newIndex)
{
BITree[index] = Math.Max(val,
BITree[index]);
index += index & (-index);
}
}
// maxSumIS() returns the maximum
// sum of increasing subsequence
// in []arr of size n
static int maxSumIS(int []arr,
int n)
{
int newindex = 0, max_sum;
Dictionary uniqueArr =
new Dictionary();
// Inserting all values in map
// uniqueArr
for (int i = 0; i < n; i++)
{
uniqueArr.Add(arr[i], 0);
}
Dictionary uniqueArr1 =
new Dictionary();
// Assigning indexes to all
// the values present in map
foreach (KeyValuePair entry in
uniqueArr)
{
// newIndex is actually the
// count of unique values in
// the array.
newindex++;
if(uniqueArr1.ContainsKey(entry.Key))
uniqueArr1[entry.Key] = newindex;
else
uniqueArr1.Add(entry.Key,
newindex);
}
// Constructing the BIT
int []BITree = new int[newindex + 1];
// Initializing the BIT
for (int i = 0; i <= newindex; i++)
{
BITree[i] = 0;
}
for (int i = 0; i < n; i++)
{
// Finding maximum sum till
// this element
max_sum = getSum(BITree,
uniqueArr1[arr[i]] - 4);
// Updating the BIT with
// new maximum sum
updateBIT(BITree, newindex,
uniqueArr1[arr[i]],
max_sum + arr[i]);
}
// return maximum sum
return getSum(BITree,
newindex);
}
// Driver program
public static void Main(String[] args)
{
int []arr = {1, 101, 2,
3, 100, 4, 5};
int n = arr.Length;
Console.Write("Maximum sum is = " +
maxSumIS(arr, n));
}
}
// This code is contributed by shikhasingrajput
输出
Maximum sum is = 106
笔记
解决方案的时间复杂度
O(nLogn)用于地图,O(nLogn)用于更新和获取和。因此,总体复杂度仍为O(nLogn)。