一个值在 [1, N] 范围内的最大成本,使得值最多位于 K 个给定范围内
给定一个正整数N和一个大小为M且类型为{L, R, C}的数组arr[] ,使得C是在[L, R]范围内选择一个元素的成本和一个正整数K ,任务是在[1, N]范围内找到一个值的最大成本,使得值最多位于 K给定范围arr[]中。
例子:
Input: arr[] = {{2, 8, 800}, {6, 9, 1500}, {4, 7, 200}, {3, 5, 400}}, K = 2
Output: 2300
Explanation:
The item chosen is 6 and the costs chosen are 0th and 1st indices such that the 6 belongs into the range of 2 to 8 and 6 to 9. Therefore the total sum of the costs 800 + 1500 is 2300, which is maximum.
Input: arr[] = {{1, 3, 400}, {5, 5, 500}, {2, 3, 300}}, K = 3
Output: 700
方法:给定问题可以通过存储位于区间范围 L 到 R 内的每个项目i的所有成本,并以非递增顺序对所选成本进行排序来解决。然后比较所有项目的前K个成本的总和。可以按照以下步骤进行:
- 初始化一个变量,比如totMaxSum = 0来存储成本的最大总和。
- 初始化一个变量,比如sum = 0来存储第i个项目的成本总和。
- 初始化一个向量,比如currCost来存储第i个项目的成本。
- 使用变量i在[1, N]的范围内进行迭代,并使用变量j在[0, M – 1]的范围内进行嵌套迭代,并执行以下步骤:
- 如果i的值位于[arr[i][0], arr[i][1]]范围内,则将arr[i][2]的值添加到向量currCost中。
- 以非递增顺序对向量currCost进行排序。
- 迭代向量currCost并将前K个元素的值添加到sum 。
- 通过max(totMaxSum, sum)更新totMaxSum的值。
- 完成上述步骤后,打印totMaxSum的值作为最大和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to maximize the total cost
// by choosing an item which lies in
// the interval L to R
void maxTotalCost(vector >& arr,
int M, int N, int K)
{
// Stores the total maximum sum
int totMaxSum = 0;
for (int i = 1; i <= N; ++i) {
// Stores the cost for ith item
vector currCost;
for (int j = 0; j < M; ++j) {
// Check if the ith item
// belongs in the interval
if (arr[j][0] <= i
&& arr[j][1] >= i) {
// Add the the jth cost
currCost.push_back(arr[j][2]);
}
}
// Sort the currCost[] in the
// non increasing order
sort(currCost.begin(),
currCost.end(),
greater());
// Stores the ith item sum
int sum = 0;
// Choose at most K costs
for (int j = 0;
j < K
&& j < currCost.size();
++j) {
// Update the sum
sum += currCost[j];
}
// Update the totMaxSum
totMaxSum = max(totMaxSum, sum);
}
// Print the value of totMaxSum
cout << totMaxSum << endl;
}
// Driver Code
int main()
{
int N = 10;
vector > arr = { { 2, 8, 800 },
{ 6, 9, 1500 },
{ 4, 7, 200 },
{ 3, 5, 400 } };
int M = arr.size();
int K = 2;
maxTotalCost(arr, M, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to maximize the total cost
// by choosing an item which lies in
// the interval L to R
static void maxTotalCost(int[][] arr,
int M, int N, int K)
{
// Stores the total maximum sum
int totMaxSum = 0;
for (int i = 1; i <= N; ++i) {
// Stores the cost for ith item
Vector currCost = new Vector();
for (int j = 0; j < M; ++j) {
// Check if the ith item
// belongs in the interval
if (arr[j][0] <= i
&& arr[j][1] >= i) {
// Add the the jth cost
currCost.add(arr[j][2]);
}
}
// Sort the currCost[] in the
// non increasing order
Collections.sort(currCost,Collections.reverseOrder());
// Stores the ith item sum
int sum = 0;
// Choose at most K costs
for (int j = 0;
j < K
&& j < currCost.size();
++j) {
// Update the sum
sum += currCost.get(j);
}
// Update the totMaxSum
totMaxSum = Math.max(totMaxSum, sum);
}
// Print the value of totMaxSum
System.out.print(totMaxSum +"\n");
}
// Driver Code
public static void main(String[] args)
{
int N = 10;
int[][] arr = { { 2, 8, 800 },
{ 6, 9, 1500 },
{ 4, 7, 200 },
{ 3, 5, 400 } };
int M = arr.length;
int K = 2;
maxTotalCost(arr, M, N, K);
}
}
// This code is contributed by shikhasingrajput
Python3
# python program for the above approach
# Function to maximize the total cost
# by choosing an item which lies in
# the interval L to R
def maxTotalCost(arr, M, N, K):
# Stores the total maximum sum
totMaxSum = 0
for i in range(1, N + 1):
# Stores the cost for ith item
currCost = []
for j in range(0, M):
# Check if the ith item
# belongs in the interval
if (arr[j][0] <= i and arr[j][1] >= i):
# Add the the jth cost
currCost.append(arr[j][2])
# Sort the currCost[] in the
# non increasing order
currCost.sort()
# Stores the ith item sum
sum = 0
# Choose at most K costs
for j in range(0, K):
# Update the sum
if j >= len(currCost):
break
sum += currCost[j]
# Update the totMaxSum
totMaxSum = max(totMaxSum, sum)
# Print the value of totMaxSum
print(totMaxSum)
# Driver Code
if __name__ == "__main__":
N = 10
arr = [[2, 8, 800],
[6, 9, 1500],
[4, 7, 200],
[3, 5, 400]
]
M = len(arr)
K = 2
maxTotalCost(arr, M, N, K)
# This code is contributed by rakeshsahni
C#
// C++ program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to maximize the total cost
// by choosing an item which lies in
// the interval L to R
static void maxTotalCost(int[, ] arr, int M, int N,
int K)
{
// Stores the total maximum sum
int totMaxSum = 0;
for (int i = 1; i <= N; ++i) {
// Stores the cost for ith item
List currCost = new List();
for (int j = 0; j < M; ++j) {
// Check if the ith item
// belongs in the interval
if (arr[j, 0] <= i && arr[j, 1] >= i) {
// Add the the jth cost
currCost.Add(arr[j, 2]);
}
}
// Sort the currCost[] in the
// non increasing order
currCost.Sort();
// Stores the ith item sum
int sum = 0;
// Choose at most K costs
for (int j = 0; j < K && j < currCost.Count;
++j) {
// Update the sum
sum += currCost[j];
}
// Update the totMaxSum
totMaxSum = Math.Max(totMaxSum, sum);
}
// Print the value of totMaxSum
Console.WriteLine(totMaxSum);
}
// Driver Code
public static void Main()
{
int N = 10;
int[, ] arr = { { 2, 8, 800 },
{ 6, 9, 1500 },
{ 4, 7, 200 },
{ 3, 5, 400 } };
int M = arr.GetLength(0);
int K = 2;
maxTotalCost(arr, M, N, K);
}
}
// This code is contributed by ukasp.
Javascript
输出:
2300
时间复杂度: O(N*M*log M)
辅助空间: O(N*M)