Maximum cost of a value over the range [1, N] such that values lies in at most K given ranges
Last Updated :
14 Jul, 2022
Given a positive integer N and an array arr[] of size M of the type {L, R, C} such that C is the cost of choosing an element in the range [L, R] and a positive integer K, the task is to find the maximum cost of a value over the range [1, N] such that values lies in at most K given range arr[].
Examples:
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
Approach: The given problem can be solved by storing all the costs for every item i which lies in the interval range L to R, and sorting the chosen costs in non-increasing order. Then comparing the sum of the first K costs of all items. Below steps can be followed:
- Initialize a variable, say totMaxSum = 0 to store the maximum sum of the cost.
- Initialize a variable, say sum = 0 to store the ith item sum of the cost.
- Initialize a vector, say currCost to store the costs for the ith item.
- Iterate over the range of [1, N] using the variable i and nested iterate over the range of [0, M – 1] using the variable j and perform the following steps:
- If the value of i lies over the range [arr[i][0], arr[i][1]], then add the value of arr[i][2] to the vector currCost.
- Sort the vector currCost in non-increasing order.
- Iterate over the vector currCost and add the values of the first K elements to sum.
- Update the value of totMaxSum by max(totMaxSum, sum).
- After completing the above steps, print the value of totMaxSum as the maximum sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxTotalCost(vector<vector< int > >& arr,
int M, int N, int K)
{
int totMaxSum = 0;
for ( int i = 1; i <= N; ++i) {
vector< int > currCost;
for ( int j = 0; j < M; ++j) {
if (arr[j][0] <= i
&& arr[j][1] >= i) {
currCost.push_back(arr[j][2]);
}
}
sort(currCost.begin(),
currCost.end(),
greater< int >());
int sum = 0;
for ( int j = 0;
j < K
&& j < currCost.size();
++j) {
sum += currCost[j];
}
totMaxSum = max(totMaxSum, sum);
}
cout << totMaxSum << endl;
}
int main()
{
int N = 10;
vector<vector< int > > 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
import java.util.*;
class GFG
{
static void maxTotalCost( int [][] arr,
int M, int N, int K)
{
int totMaxSum = 0 ;
for ( int i = 1 ; i <= N; ++i) {
Vector<Integer> currCost = new Vector<Integer>();
for ( int j = 0 ; j < M; ++j) {
if (arr[j][ 0 ] <= i
&& arr[j][ 1 ] >= i) {
currCost.add(arr[j][ 2 ]);
}
}
Collections.sort(currCost,Collections.reverseOrder());
int sum = 0 ;
for ( int j = 0 ;
j < K
&& j < currCost.size();
++j) {
sum += currCost.get(j);
}
totMaxSum = Math.max(totMaxSum, sum);
}
System.out.print(totMaxSum + "\n" );
}
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);
}
}
|
Python3
def maxTotalCost(arr, M, N, K):
totMaxSum = 0
for i in range ( 1 , N + 1 ):
currCost = []
for j in range ( 0 , M):
if (arr[j][ 0 ] < = i and arr[j][ 1 ] > = i):
currCost.append(arr[j][ 2 ])
currCost.sort()
sum = 0
for j in range ( 0 , K):
if j > = len (currCost):
break
sum + = currCost[j]
totMaxSum = max (totMaxSum, sum )
print (totMaxSum)
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)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void maxTotalCost( int [, ] arr, int M, int N,
int K)
{
int totMaxSum = 0;
for ( int i = 1; i <= N; ++i) {
List< int > currCost = new List< int >();
for ( int j = 0; j < M; ++j) {
if (arr[j, 0] <= i && arr[j, 1] >= i) {
currCost.Add(arr[j, 2]);
}
}
currCost.Sort();
int sum = 0;
for ( int j = 0; j < K && j < currCost.Count;
++j) {
sum += currCost[j];
}
totMaxSum = Math.Max(totMaxSum, sum);
}
Console.WriteLine(totMaxSum);
}
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);
}
}
|
Javascript
<script>
function maxTotalCost(arr,
M, N, K)
{
let totMaxSum = 0;
for (let i = 1; i <= N; ++i) {
let currCost = [];
for (let j = 0; j < M; ++j) {
if (arr[j][0] <= i
&& arr[j][1] >= i) {
currCost.push(arr[j][2]);
}
}
currCost.sort( function (a, b) { return b - a })
let sum = 0;
for (let j = 0;
j < K
&& j < currCost.length;
++j) {
sum += currCost[j];
}
totMaxSum = Math.max(totMaxSum, sum);
}
document.write(totMaxSum + '<br>' );
}
let N = 10;
let arr = [[2, 8, 800],
[6, 9, 1500],
[4, 7, 200],
[3, 5, 400]];
let M = arr.length;
let K = 2;
maxTotalCost(arr, M, N, K);
</script>
|
Time Complexity: O(N*M*log M)
Auxiliary Space: O(N*M)
Share your thoughts in the comments
Please Login to comment...