长度K的最大和子序列|设置 2
给定一个数组序列arr[]即[A 1 , A 2 ...A n ]和一个整数k ,任务是找到长度为k的递增子序列S 的最大可能和,使得S 1 <=S 2 <=S 3 ………<=S k 。
例子:
Input: arr[] = {-1, 3, 4, 2, 5}, K = 3
Output: 3 4 5
Explanation: Subsequence 3 4 5 with sum 12 is the subsequence with maximum possible sum.
Input: arr[] = {6, 3, 4, 1, 1, 8, 7, -4, 2}
Output: 6 3 4 8 7
方法:可以使用贪婪方法来解决任务。这个想法是从子序列中的arr[]中获取最大可能的元素。请按照以下步骤解决问题。
- 声明一个对容器的向量,比如使用 []来存储元素及其索引。
- 遍历arr[]并推送所有使用中的元素 []及其索引。
- 以非递减顺序对use[]进行排序。
- 声明一个向量ans[]来存储最终的子序列。
- 使用 i 遍历use[]并从 use 中取出最后一个K元素并将它们的索引( use[i].second )推入ans[] 。
- 以非递减顺序对ans[]进行排序,以便索引应按递增顺序排列。
- 现在用 i 遍历ans[]并用arr[ans[i]]替换每个元素。
- 返回ans[]作为最终的最大和子序列。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to find the subsequence
// with maximum sum of length K
vector maxSumSubsequence(vector& arr, int N,
int K)
{
// Use an extra array to keep
// track of indices while sorting
vector > use;
for (int i = 0; i < N; i++) {
use.push_back({ arr[i], i });
}
// Sorting in non-decreasing order
sort(use.begin(), use.end());
// To store the final subsequence
vector ans;
// Pushing last K elements in ans
for (int i = N - 1; i >= N - K; i--) {
// Pushing indices of elements
// which are part of final subsequence
ans.push_back(use[i].second);
}
// Sorting the indices
sort(ans.begin(), ans.end());
// Storing elements corresponding to each indices
for (int i = 0; i < int(ans.size()); i++)
ans[i] = arr[ans[i]];
// Return ans as the final result
return ans;
}
// Driver Code
int main()
{
int N = 9;
vector arr = { 6, 3, 4, 1, 1, 8, 7, -4, 2 };
int K = 5;
// Storing answer in res
vector res = maxSumSubsequence(arr, N, K);
// Printing the result
for (auto i : res)
cout << i << ' ';
return 0;
}
Java
// Java program for above approach
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class GFG {
static class Pair {
int first;
int second;
Pair(int i, int j) {
this.first = i;
this.second = j;
}
}
// Function to find the subsequence
// with maximum sum of length K
static ArrayList maxSumSubsequence(int[] arr, int N, int K) {
// Use an extra array to keep
// track of indices while sorting
ArrayList use = new ArrayList();
for (int i = 0; i < N; i++) {
use.add(new Pair(arr[i], i));
}
// Sorting in non-decreasing order
Collections.sort(use, new Comparator() {
@Override
public int compare(Pair i, Pair j) {
return i.first - j.first;
}
});
// To store the final subsequence
ArrayList ans = new ArrayList();
// Pushing last K elements in ans
for (int i = N - 1; i >= N - K; i--) {
// Pushing indices of elements
// which are part of final subsequence
ans.add(use.get(i).second);
}
// Sorting the indices
Collections.sort(ans);
// Storing elements corresponding to each indices
for (int i = 0; i < ans.size(); i++)
ans.set(i, arr[ans.get(i)]);
// Return ans as the final result
return ans;
}
// Driver Code
public static void main(String args[]) {
int N = 9;
int[] arr = { 6, 3, 4, 1, 1, 8, 7, -4, 2 };
int K = 5;
// Storing answer in res
ArrayList res = maxSumSubsequence(arr, N, K);
// Printing the result
for (int i : res)
System.out.print(i + " ");
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# python program for above approach
# Function to find the subsequence
# with maximum sum of length K
def maxSumSubsequence(arr, N, K):
# Use an extra array to keep
# track of indices while sorting
use = []
for i in range(0, N):
use.append([arr[i], i])
# Sorting in non-decreasing order
use.sort()
# To store the final subsequence
ans = []
# Pushing last K elements in ans
for i in range(N - 1, N - K - 1, -1):
# Pushing indices of elements
# which are part of final subsequence
ans.append(use[i][1])
# Sorting the indices
ans.sort()
# Storing elements corresponding to each indices
for i in range(0, len(ans)):
ans[i] = arr[ans[i]]
# Return ans as the final result
return ans
# Driver Code
if __name__ == "__main__":
N = 9
arr = [6, 3, 4, 1, 1, 8, 7, -4, 2]
K = 5
# Storing answer in res
res = maxSumSubsequence(arr, N, K)
# Printing the result
for i in res:
print(i, end=' ')
# This code is contributed by rakeshsahni
C#
// C# program for above approach
using System;
using System.Collections.Generic;
public class GFG {
class Pair {
public int first;
public int second;
public Pair(int i, int j) {
this.first = i;
this.second = j;
}
}
// Function to find the subsequence
// with maximum sum of length K
static List maxSumSubsequence(int[] arr, int N, int K) {
// Use an extra array to keep
// track of indices while sorting
List use = new List();
for (int i = 0; i < N; i++) {
use.Add(new Pair(arr[i], i));
}
// Sorting in non-decreasing order
use.Sort((i, j) => i.first - j.first);
// To store the readonly subsequence
List ans = new List();
// Pushing last K elements in ans
for (int i = N - 1; i >= N - K; i--) {
// Pushing indices of elements
// which are part of readonly subsequence
ans.Add(use[i].second);
}
// Sorting the indices
ans.Sort();
// Storing elements corresponding to each indices
for (int i = 0; i < ans.Count; i++)
ans[i] = arr[ans[i]];
// Return ans as the readonly result
return ans;
}
// Driver Code
public static void Main(String []args) {
int N = 9;
int[] arr = { 6, 3, 4, 1, 1, 8, 7, -4, 2 };
int K = 5;
// Storing answer in res
List res = maxSumSubsequence(arr, N, K);
// Printing the result
foreach (int i in res)
Console.Write(i + " ");
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
6 3 4 8 7
时间复杂度: O(NlogN),其中 N 是数组的大小
辅助空间: O(N),其中 N 是数组的大小